Posts

Showing posts with the label Asynchronous programming

Asynchronous Programming Concepts (.NET related)

Async execution - It's all about responsiveness. Old code scenarios To use WaitHandle from a Task. Implement the APM pattern by FromAsync methods and "Tasks Forest" Implement the EPM pattern by TaskCompletionSource. Asynchronous Task means the Threadless Task. Asynchronous Tasks may have only the next statuses: Fault, RunToCompletion, Cancelled. Continuation for Asynchronous Tasks Use the TaskCompletionSource<T> class. async await A syntactic sugar for awful amount of work. A compiler's underground job. A enumeration, try, finally, using and etc. Users SynchronizationContext implicitly. Unwrap multiple exceptions The await will return only the first exception from a AggregateException. There is a useful trick to unwrap multiple exceptions. await task.ContinueWith(     ( ) => { } ,     TaskContinuationOptions. ExecuteSynchronously ); var results = task .Result; Multiple await Preventing of lost exceptions wit...

Concurrent Programming

Asynchronous Programming Parallel Programming Multithreaded Programming

How to invoke an event handlers asynchronously

How to invoke an event or delegate handlers asynchronously Variant with the  Parallel class (TPL). public void OnMyEvent( String myValue) {     var multiCastDelegate = Interlocked .CompareExchange( ref MyEvent, null , null );     if (multiCastDelegate == null )      {         return ;      }     var handlers = multiCastDelegate.GetInvocationList()         .Cast< EventHandler < MyEventArgs >>();     var args = new MyEventArgs (myValue);     Parallel .ForEach(         handlers,         handler => handler( this , args)); }   Old variant with the Asynchronous delegates (APM). public void OnMyEventOld( String myValue) {     var multiCastDelegate = Interlocked .CompareExc...

The .NET Asynchrony And Multithreading lecture (in progress, not finished)

Plain Threading     Thread.Sleep(0) vs Thread.Yield()     Foreground and Background Threads     finally blocks in the execution stack of background threads are circumvented         (including using)     Thread Priority     Memory-mapped files     Exception Handling         Swallow of an exception in the main thread.         Centralized exception handling             global event             only for an UI thread             AppDomain.CurrentDomain.UnhandledException                 app will shutdown or <legacyunhandledexceptionpolicy> Thread Pooling     Thread.CurrentThread.IsThreadPoolThread     Oversubscription         hill-climbing algorithm   ...