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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With