Wait until a delegate is called

I have an asynchronous class with a method StartProcessing()that raises an event int ResultReady()when it has finished processing. StartProcessing()takes very little time.

I want to call this class synchronously. My pseudo code should look something like this:

  • Call StartProcessing ()

  • Waiting / hibernating until the result is ready

  • Return result

Which design template is best for this? Could you give me some sample code?

+3
source share
1 answer

- ManualResetEvent, . Set WaitOne ( ) . , STA, WinForms ( STA), .

- :

var async = new AsyncClass();
var manualEvent = new ManualResetEvent();
async.ResultReady += args => manualEvent.Set();
async.StartProcessing();
manualEvent.WaitOne();
+5

Source: https://habr.com/ru/post/1791929/


All Articles