Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using a thread to manage data updates
Message
General information
Forum:
ASP.NET
Category:
.NET Compact Framework
Title:
Using a thread to manage data updates
Environment versions
Environment:
C# 1.1
OS:
Windows XP SP2
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01058024
Message ID:
01058024
Views:
59

Please ignore the problem below.
I have awarded myself the "BoneHead Of The Day" award for gettied stymied by a completely obvious, stupid, stupid mistake.!

Have a nice day!








I am working a Pocket PC client for a VFP application. I am using VFP Web Services to send/receive data. In order to keep the application responsive and manage possible disconnected periods, I am trying to create a separate thread to handle all communications with the web server. I have created a class that includes a message queue form an ArrayList which I can add items to from my primary thread, and then my worker thread checks the queue periodically and sends the appropriate data.

The problem is that it doesn't seem to be actually running a separate thread. From the call that adds the item to the queue, processing in my primary thread halts until the function in the worker thread completes.

So, what is the best architecture for doing this. Can I have a global, static ArrayList for a message queue which is accessible to both threads? Or do I need to do something else.

Here is a class that defines the thread. It is created as a global static instance.
using System;
using System.Threading;
using System.Collections;
using System.Data;
using System.Xml;

namespace BSS.PocketServer
{
	public enum DataMessageAction
	{
		NewTicket,
		AddItem
	}

	public struct DataMessage 
	{
		public DataMessageAction Action;
		public int TranNbr;
		public string Identifier;
	}


	/// <summary>
	/// Monitors for transactions which need to be submitted to BPA via the web service.
	/// Receives results and updates local data tables appropriately.
	/// </summary>
	public class DataLink
	{
		const int SLEEPINTERVAL = 250;

		public bool abortflag = false;
		public ArrayList alQueue = new ArrayList();

		private Thread threadMonitor;

		public DataLink()
		{
			//
			// TODO: Add constructor logic here
			//
			ThreadStart ts = new ThreadStart(MonitorThread);
			threadMonitor = new Thread(ts);
			threadMonitor.Start();
		}

		//
		// Add an item to the send queue
		//
		public void AddItem(DataMessageAction Action, int TranNbr, string Identifier)
		{
			DataMessage dm = new DataMessage();
			dm.Action = Action;
			dm.TranNbr = TranNbr;
			dm.Identifier = Identifier;
			lock(alQueue)
			{
				alQueue.Add(dm);
			}
		}


//---------------------------------------------------------------
// Thread functions
//---------------------------------------------------------------

		//
		// Primary thread loop.  
		// Process all outstanding messages.  If there are none, go to sleep.
		//
		public void MonitorThread()
		{
			do 
			{
				if (alQueue.Count > 0)
				{
					DataMessage dm = (DataMessage)alQueue[0];


// ...... Processing code to call web service here 
//        the following code simulates some processing .........

Thread.Sleep(5000);		

System.Windows.Forms.MessageBox.Show("Finished - Data thread");



				}

				else
				{
					Thread.Sleep(SLEEPINTERVAL);
				}

			} while (abortflag == false);
		}

	}
}
Messages are sent using the following code:
			//
			// Send message to DataLink thread
			//
			DataMessage dm;
			dm.Action = DataMessageAction.AddItem;
			dm.Identifier = iNewCount.ToString();
			dm.TranNbr = m_tran_nbr;

			Global.oDataLink.SendMessage(dm);

			MessageBox.Show("Finished - main thread");
Paul R. Moon
Business Software Solutions
paul@businessoftware.com
Reply
Map
View

Click here to load this message in the networking platform