0

I have a DLL that return all information asynchronous to several event handler. When I press a button I call a series of functions in the DLL, but the next function should not be called until the previous function has fired the event handler.

Button_Click_Handler() {
  LCV.Load() // Returns immediately, Fires OnLoaded or OnError when done.
  // Pause execution here and wait the for OnLoaded or OnError event to fire.
  LCV.Read() // Returns immediately, Fires OnCard Or OnError when done.
  // Pause execution here and wait the for OnCard or OnError event to fire.
  LCV.Put(x) // Returns immediately, Fires OnPut Or OnError when done.
  // Pause execution here and wait the for OnPut or OnError event to fire.
}

Is there a way to wait for an event to fire without having to attach an event handler and use signalling? To spread out the flow through all event handler would be very messy and not easy to follow. If the code was in the event handlers themself, it has to know "what mode" it is in and execute different code snippets. As I said, extremely messy.

Max Kielland
  • 5,627
  • 9
  • 60
  • 95
  • Normally you should use a semaphore like WaitOne : https://learn.microsoft.com/en-us/dotnet/api/system.threading.waithandle.waitone?view=netframework-4.7.2 I like the socket asynchronous Server as a good example : https://learn.microsoft.com/en-us/dotnet/framework/network-programming/socket-code-examples – jdweng Mar 25 '19 at 13:39
  • "Is there a way to wait for an event to fire without having to attach an event handler and use signalling?" Not really, but here's a relatively simple solution by Adam using [TaskCompletionSource](https://stackoverflow.com/a/38225927/2330053). – Idle_Mind Mar 25 '19 at 14:12

0 Answers0