F-Async Programming Patterns

Fire and Forgot

Generally used with thread pool , Main thread submit the job and never care about the result

Blocking wait (Fork And Join)

This is used when a limited number of threads are created by main thread and main thread has nothing to other that waiting for complete.
In this case main thread start one or more child thread and call Thread.Join() for each thread started. 

Pooling (Spin Wait)

This mechanism is mainly used with thread pool. Main Thread fires multiple task (Async Work-item) to thread pool then wait for complete and a spin wait patterns. Be ware that because thread pool do not have any facility to notify the progress so special mechanism is required to collect the complete status of task submitted.

Usually I prefer flowing mechanism
  1. Wrap your ThreadProc in an instance  that must have a public property ISComplete
  2. Push all the task instances to a List Instance.
  3. Loop through list and submit the Instance.ThreadProc() to Thread pool.
  4. Write a busy wait while loop that iterate through entire list periodically querying Instance.ISComplete   property.
  5. If any of the ISComplete=False then skip rest and wait for next iteration
  6. If all instances completed then Exit the busy wait loop.

Callback

Call back is always best way to get notified about completion , it works every where.  It is mainly suitable when you started multiple task and want to get notified about the status each single thread.
Downside of this model is that it makes debugging difficult. 

Batch Complete Notification

Downside of Pooling (Spin wait) is that this mechanism will keep main thread busy if you don`t want this then Create a background Worker thread and put entire While loop logic to Worker.DoWork event handler and all a notification call back for main thread. 



Comments