-3

I have this code :

serialPort.PinChanged +=Func5;
Func1();
Func2();
//here event throw
Func3();
Func4();
Console.ReadKey();

When I enter to Func5? Only when my thread will not have what to do?(after the last line)?

Or when the event throw? Is that depend witch type of event is that?

How can I set that I want to catch this event immediately ?

  • 2
    Events aren't "thrown" or "caught" (those terms apply to exceptions, not events). If you are asking when the `Func5()` method will be called, it's impossible for anyone here to tell you that, as it depends entirely on how the event is implemented, which your question doesn't show at all. You need to provide a good [mcve] that clearly illustrates your question (are you [seeing a pattern](https://stackoverflow.com/questions/47971746/c-sharp-eventhandler-on-different-thread) here?). – Peter Duniho Dec 25 '17 at 23:35

3 Answers3

1

If the event is triggered asynchronously (that is, outside the flow control of the displayed code) there is no way to know when the event will run in relation to the shown code. It may not even run on the same thread as the shown code.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • can I configure on my code to listening to this event and handle it on another thread , on that way i catch this event immediately? – JORDANgintod Dec 26 '17 at 04:30
0

It depends on the type of application and whether the event is risen in the same thread.

Let's assume that you have a WinForms application, that the code shown above is running from a Click-event and that the PinChanged event is triggered in the same thread, i.e. the UI-thread.

WinForms never interrupts your running code. If you have a long running Click method and the user is clicking a second time before the method is terminated, then the second click event is put into a wait queue and the first click method continues until it is terminated. This also means that the long running method is blocking the UI. On the other hand, it makes things much simpler, as you don't have to deal with code blocks interrupting each other or running at the same time, what could be very tricky.

The same is true for your PinChanged event. It will only run Func5 when your application is idle, i.e. all other code is terminated.

Again, this is only true if you or the library functions involved do not use multithreading. It is unlikely that PinChanged will be risen in another thread, unless the documentation explicitly states it.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • can I configure on my code to listening to this event and handle it on another thread , on that way i catch this event immediately? – JORDANgintod Dec 26 '17 at 04:30
  • No, the code rising the event determines the thread. But you could start the serial port listener on another thread. Don't forget to [Invoke](https://stackoverflow.com/questions/6650691/invoke-in-windows-forms) the UI from the event listener then being run on this other thread, – Olivier Jacot-Descombes Dec 26 '17 at 16:24
0

Your Func5 will be called only when the serial port pin changes, which is the event to which the function is subscribed. This may happen before the call to Func1 or maybe never... it all depends on two factors:

  • if the event occurs
  • when the event occurs

If you can change the serial port pin programmatically, then you have full control over the event and you can basically make the event fire whenever you want... otherwise the occurrence is located outside the scope of your control.

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98