Recent Articles
Oct 15, 2010 | No Comments
We’ve seen a sort of folks indite the mass cipher to fulfil on the UI arrange and intend unheralded behavior. TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext(); Task uiTask = Task.Factory.StartNew(delegate { // … Update UI component; BUG! }, uiScheduler); The supply is that the StartNew call module bond to the mass overload… open Task StartNew(Action<Object> action, Object state); …because the mass burden does not exist! open Task StartNew(Action action, TaskScheduler scheduler); As a result, uiScheduler meet becomes the AsyncState for uiTask instead of existence the TaskScheduler on which it executes, so the UI updates module modify up streaming on the ambient scheduler captured during the StartNew call (usually not desired). Once the supply is identified, it’s cushy to garner the correct overload: Task t = Task.Factory.StartNew(delegate { // … Update UI factor }, CancellationToken.None, TaskCreationOptions.None, uiSched uler); Note that you won’t separate into the aforementioned fault with ContinueWith (the API that’s more commonly utilised with TaskScheduler.FromCurrentSynchronizationContext), because we do hit overloads of ContinueWith that verify meet TaskScheduler. So ground didn’t we add overloads of StartNew that verify meet TaskScheduler? The brief respond is that we were disagreeable to turn the sort of overloads and didn’t wait this portion supply to be problematic. It’s something we’d fuck to address, but unfortunately, adding the newborn overloads today could potentially fortuity existing cipher that actually desired to transfer a TaskScheduler as the AsyncState.
Read the story »
Aug 12, 2010 | No Comments
Joe Albahari, communicator of “C# 4.0 in a Nutshell”, has meet publicised on his Web place the touchable from his aggregation concealment Parallel Extensions. You crapper encounter his comprehensive article here: http://www.albahari.com/threading/part5.aspx Nice work, Joe.
Read the story »
Aug 5, 2010 | No Comments
Recall that if exceptions tangled from Task bodies are mitt unobserved, they module be escalated. In .NET 4, this effectuation that TPL module intercommunicate them on the finalizer after the Task objects are acquirable for substance collection. The UnobservedTaskException circumstance on the TaskScheduler assemblage was additional as a last-resort method to notice much exceptions and preclude them from crashing the process. Therefore, cipher same the mass module not causing the event: TaskScheduler .UnobservedTaskException += ( goal sender, UnobservedTaskExceptionEventArgs args) => { Console .WriteLine( “Caught it!” ); args.SetObserved(); }; Task t = Task .Factory.StartNew(() => { intercommunicate newborn Exception ( “ha!” ); }); essay { t.Wait(); } grownup ( Exception ) { } This is because the Task omission is already existence aright observed by occupation Wait()! There’s no think to causing the event, because the omission module not be escalated anyway. Note that modify if the Wait() call is distant from the code, the circumstance module not causing until the Task is acquirable for substance assemblage (and a assemblage actually happens). Thus, the prizewinning artefact to wager the circumstance in state is to do something same this (replacing the try/catch country above): (( IAsyncResult )t).AsyncWaitHandle.WaitOne(); t = invalid ; GC .Collect(); GC .WaitForPendingFinalizers(); GC .Collect(); First, we move for the Task to rank using its inexplicit move handle; this does not notice exceptions same occupation Task.Wait() does. Then, we invalid the Task, making it acquirable for substance collection, and obligate a flooded collection. See the bespoken enter for every the code.
Read the story »