When do asynchronous delegates use a callback pattern?

One of the patterns used with asynchronous delegates is also a callback pattern where the initial thread (one called BeginInvoke) of T1 continues without waiting or checking if the spawned T2 thread is completed. Instead, when T2 is completed, T2 calls a callback method that processes the results, calls EndInvoke, and informs T1 about the completion of the task.

a) If the callback method should inform T1 when the task is completed, then why isnt this callback method inside T1 and not T2?

2) Is there some kind of standard pattern, how should the callback method inform T1 of the completion of T2?

3) Should I use a callback pattern even if T1 should get the return value of the asynchronously called method?

thank

+3
source share
2 answers
  • Why is the callback method not called in T1?

This is usually not possible; if T1 is turned off while performing some other work, there is no way to return work to it if the stream already has no mechanism for publishing and a schedule for working on it (for example, the user interface stream through SynchronizationContext).

  • Is there a standard template for cross-stream notification?

I would say no; There are several cross-thread synchronization patterns, each of which applies to different target scenarios.

  • , T1 ?

T1 , . , EndInvoke, WaitHandle .

, , " " (, ), /, SynchronizationContext.Post Dispatcher.Invoke , .

+3

. Windows Forms WPF, . , Control.Invoke Dispatcher.Invoke. , BackgroundWorker. , RunWorkerCompleted.

, , , BeginInvoke(), .

+3

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


All Articles