Click here to Skip to main content
15,924,828 members
Home / Discussions / C#
   

C#

 
GeneralRe: Finally in threads Pin
TylerBrinks10-Mar-05 11:52
TylerBrinks10-Mar-05 11:52 
GeneralRe: Finally in threads Pin
S. Senthil Kumar10-Mar-05 20:05
S. Senthil Kumar10-Mar-05 20:05 
Generalstring foreach row's column Pin
lornej10-Mar-05 11:05
lornej10-Mar-05 11:05 
GeneralRe: string foreach row's column Pin
Colin Angus Mackay10-Mar-05 11:30
Colin Angus Mackay10-Mar-05 11:30 
GeneralRe: string foreach row's column Pin
J4amieC10-Mar-05 22:14
J4amieC10-Mar-05 22:14 
General.NET remoting and Simple Client / Server application design help... Pin
Kasdoffe10-Mar-05 10:50
Kasdoffe10-Mar-05 10:50 
GeneralRe: .NET remoting and Simple Client / Server application design help... Pin
Kasdoffe11-Mar-05 3:30
Kasdoffe11-Mar-05 3:30 
GeneralAsync Programming and Thread-safe Delegates Pin
BnWasteland10-Mar-05 9:29
BnWasteland10-Mar-05 9:29 
I am working on a socket-based client application, and I need a facility to ensure that a given delegate will execute only on the main GUI thread.

I understand that derivatives of System.Windows.Forms.Control achieve this by implementing ISynchronizeInvoke, and outside threads can call Invoke or BeginInvoke/EndInvoke to run code on the thread which "owns" the control. I need similar functionality, but in a class that is clearly not a Control derivative.

In the code below, I would like to attach a notification to the end of the ReceiveCallback() method to inform the main thread that data is ready in the receive queue.

I see 3 ways in which this can be accomplished:

1) Override the main WndProc routine, and check MessagesPending() on each iteration through the message loop. I'm not extremely familiar with multithreading issues, but it seems that this would cause the queue to be locked most of the time, not to mention burning unneccesary cycles in the core program loop.

2) Post a system message and respond to it in the main WndProc loop. I've seen ways to respond to the message via an IMessageFilter object, but I have not been able to find a means of posting a custom message to the main window.

3) Implement a system like the controls use, involving the ISyncronizeInvoke interface. However, I am completely at a loss as to how one could achieve this functionality.

Any help is greatly appreciated. If I can work this out, I will most likely put together an article covering the topic and submit it to the code project.

Here's a sample of the class code:

<code>
public class CommunicationManager
{
	public delegate void CMEventHandler();
	public event CMEventHandler DataReceived;

	private ManualResetEvent ReadyToStop = new ManualResetEvent(false);
	private AutoResetEvent ReadyToSend = new AutoResetEvent(false);
	private AutoResetEvent ReadyToRecv = new AutoResetEvent(false);

	private Socket sock;
	private Queue inQ, outQ;

	public CommunicationManager() {}
	public bool MessagesPending() {}
	public string GetMessage() {}
	public void SendMessage(string msg) {}
	public void Connect(string IP, int Port) {}
	public void Disconnect() {}

	private void ReceiveThreadEntryPoint(object state)
	{
		WaitHandle[] handles = new WaitHandle[2];
		handles[0] = ReadyToStop;
		handles[1] = ReadyToRecv;
		StateObject so = new StateObject();

		while(true) 
		{
			if (sock != null && sock.Connected) 
			{
				so.Clear();
				so.sock = sock;
				sock.BeginReceive(so.buff, 0, StateObject.BUFFER_SIZE, 0, new AsyncCallback(ReceiveCallback), so);
			}				

			if (WaitHandle.WaitAny(handles) == 0)
				break;
		}
	}

	private void SendThreadEntryPoint(object state)
	{
		while(true)
		{
			WaitHandle[] handles = new WaitHandle[2];
			handles[0] = ReadyToStop;
			handles[1] = ReadyToSend;

			if( WaitHandle.WaitAny(handles) == 0 )
			{
				break;
			}

			if (sock != null && sock.Connected) 
			{
				while(outQ.Count > 0) 
				{
					StateObject so = new StateObject();
					so.sock = sock;
					byte[] buff = Encoding.ASCII.GetBytes((string)outQ.Dequeue());
					sock.BeginSend(buff, 0, buff.Length, 0, new AsyncCallback(SendCallback), so);
				}
			}

		}
	}
	private void ReceiveCallback(IAsyncResult ar)
	{
		StateObject so = (StateObject)ar.AsyncState;
		Socket s = so.sock;

		try
		{
			// sanity check
			if (s == null || !s.Connected) return;

			//Finishes the async read operation, gets number of bytes received
			int read = s.EndReceive(ar);
			if (read > 0)
			{
				//read not empty

				//post the message to the inbound queue
				string msg = Encoding.ASCII.GetString(so.buff, 0, read);
				inQ.Enqueue(msg);

				//receive complete, set up the next one
				ReadyToRecv.Set();
			}
		}
		catch {}

	}

	private void SendCallback(IAsyncResult ar)
	{
		StateObject so = (StateObject)ar.AsyncState;
		Socket s = so.sock;

		try
		{
			// sanity check
			if (s == null || !s.Connected) return;

			// finish the async send operation
			int sent = s.EndSend(ar);
		}
		catch {}
	}

	private class StateObject {}
}
</code>


-Bn. Wasteland
GeneralRe: Async Programming and Thread-safe Delegates Pin
TylerBrinks10-Mar-05 11:43
TylerBrinks10-Mar-05 11:43 
Generalnew Enum Pin
Dean Goodman10-Mar-05 8:57
Dean Goodman10-Mar-05 8:57 
GeneralRe: new Enum Pin
TylerBrinks10-Mar-05 11:32
TylerBrinks10-Mar-05 11:32 
GeneralGlobal Mouse Hook for XButtons Pin
AnzelVincir10-Mar-05 8:40
AnzelVincir10-Mar-05 8:40 
Generalnew issue - compare data Table Column Pin
lornej10-Mar-05 8:20
lornej10-Mar-05 8:20 
GeneralRe: new issue - compare data Table Column Pin
TylerBrinks10-Mar-05 8:34
TylerBrinks10-Mar-05 8:34 
GeneralRe: new issue - compare data Table Column Pin
lornej10-Mar-05 9:34
lornej10-Mar-05 9:34 
Generalproblem whit date Help Me Pin
kings_110-Mar-05 8:06
kings_110-Mar-05 8:06 
GeneralRe: problem whit date Help Me Pin
TylerBrinks10-Mar-05 8:33
TylerBrinks10-Mar-05 8:33 
GeneralRe: problem whit date Help Me Pin
kings_110-Mar-05 11:15
kings_110-Mar-05 11:15 
GeneralRe: problem whit date Help Me Pin
TylerBrinks10-Mar-05 11:21
TylerBrinks10-Mar-05 11:21 
GeneralRe: problem whit date Help Me Pin
kings_111-Mar-05 7:24
kings_111-Mar-05 7:24 
GeneralCleaning a Place Holder from a class Pin
see0710-Mar-05 6:48
see0710-Mar-05 6:48 
GeneralRe: Cleaning a Place Holder from a class Pin
see0711-Mar-05 11:41
see0711-Mar-05 11:41 
GeneralRe: Cleaning a Place Holder from a class Pin
see0714-Mar-05 5:19
see0714-Mar-05 5:19 
GeneralUse C# for Addin in Excel Pin
Fardoche610-Mar-05 6:37
Fardoche610-Mar-05 6:37 
GeneralRe: Use C# for Addin in Excel Pin
Skynyrd10-Mar-05 6:55
Skynyrd10-Mar-05 6:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.