-
February 24th, 2009, 11:05 AM
#1
Cross-Threading
Im a bit confused with sending information between threads.
I have a thread thats constantly checking a UDP port for incoming packets. When a packet arrive i need it to send the packet to one of several different classes thats run by other threads that decodes the information.
Im experiencing an issue where the program stops recieving packets while its decoding which lead me to believe that the UDP-thread also do the decoding.
Right now im using an event to send the packet:
public event PacketToProcessDelegate OnPacketToProcess;
and i use it:
if (recvBytes != 0)
{
// Decode incoming packet
OnPacketToProcess(dataBuffer);
}
So my question is: On what thread is the event handled?
Thanks!
-
February 24th, 2009, 02:10 PM
#2
Re: Cross-Threading
It depends on how things are set up.
Try displaying a trace message for the thread id to see if the event handler matches your current thread id. Use the System.Thread.CurrentThread.ManagedThreadId property.
-
February 24th, 2009, 02:52 PM
#3
Re: Cross-Threading
If it's a standard event with standard event handlers, then yes, it's the same thread.
Code:
public event EventHandler MyEvent;
public void Method ()
{
// This event handler will be executed on the same thread as invocation in 'Whatever'
MyEvent += delegate { Console.WriteLine ("I'm on the same thread"); };
}
public void Whatever ()
{
// Raise 'MyEvent' on the same thread
MyEvent (this, EventArgs.Empty);
// Raise 'MyEvent' on a different thread
Threadpool.QueueUserWorkDelegate (delegate {
MyEvent (this, EventArgs.Empty);
});
}
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|