Reputation: 13
I am trying to learn networking by making my own little network library. Currently I have a list of new clients that a code like this can use. The NewClientConnected() method returns true if there's anything in the list and removes the first element.
ConnectedClient newClient; // ConnectedClient is my class storing it's socket and stream and all that.
if(NewClientConnected(out newClient)
{
...handling new client, notification, etc...
}
Same goes for checking for new packets (prefixed slice of stream). I sought to take it to the next level and attempt to raise events when things like this happen and started with new connections. Problem is, the event is raised from another thread, causing an exception as the event is handled in the form1.cs.
How can I make sure the event is handled by the main thread from a static non-control class? Should I just keep doing what I do? (snippet above) I hear the idea of a consumer-produces relationship, it would still require a timer (uses its own thread) or another thread if I recall correctly.
I have tried to google it and look on here, but everyone seems to have a different problems. It is in a library project while another projects (my server and client test) is referring to it.
Thanks in advance.
Upvotes: 0
Views: 811
Reputation: 2118
If the operations you want to perform interact with the user interface then you have to invoke them on the UI thread.
So instead of handling the event directly, try this:
// Somewhere in the form1 code:
Server.newConnectionEvent += ConnectionEVentHandler(myMethod)
public void myMethod()
{
//Event method called from another thread
//can only do things here that do affect the UI!
this.Dispatcher.Invoke(CalledOnUIThread);
}
public void CalledOnUIThread()
{
//Handle event on UI thread here
//Can do things here that affect the UI
}
This code has to run from the form1 class, since it uses 'this'. So all you change (presumably, you didn't show your event handler code) is put the actual code that you want to run in a seperate method and then invoke that seperate method from the event handler.
See http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx for the API documentation on the dispatcher class.
See What is the definition of a UI thread? Is there only one UI thread in a .NET application? for a simple explanation of the UI thread.
Upvotes: 1
Reputation: 1647
Take a look at Control.Invoke and Control.BeginInvoke methods.
Control.Invoke - "Executes the specified delegate on the thread that owns the control's underlying window handle."
http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.begininvoke.aspx
This should fix your "another thread exception"
Upvotes: 0