Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

events and threading

suppose I have:

ethernet_adapter.PacketArrived += (s, e) => 
{
    //long processing...
};

processing may take a long time and while it was in the middle another packet has arrived. What happens next: the processing gets done and then another event is fired or perhaps new event is fired immediately but on a new thread?

like image 866
ren Avatar asked Oct 06 '22 12:10

ren


2 Answers

You should not assume. It could be anything depending upon how event is raised by type (of ethernet_adapter object).

If it is synchronous operation, new event will not be raised until current operation is in progress.

If it is asynchronous operation, new event will be raised immediately.

like image 146
Tilak Avatar answered Oct 08 '22 02:10

Tilak


Chances are this is a synchronous operation. The only way it is going to happen on another thread is if the object that raises the event does so on another thread, or if you do in the handler. There are numerous ways to do this, but using System.Threading.Tasks.Task would generally be preferred if using .NET 4.

Consider carefully how you want your application to behave. Simply processing each packet on a new thread could cause packets to be handled out of order. You may want to queue them up and have a background thread handle them at that point. Or you may not need to do anything at all.

like image 35
Jon B Avatar answered Oct 08 '22 03:10

Jon B