I am reading a book about the C # Task Parallel Library and on the following example are the Task Scheduler. Upgrade task exception handler is never triggered, can anyone give me some clues?
Task scheduler. Upgrade Task Requests + = (Object Sender, Unaffected Task Exception Event Agent eventArgs) = & gt; {EventArgs.SetObserved (); ((Overall abstraction) eventArgs.Exception). Hondol (ex = & gt; {console. WrightLine ("exception type: {0}", ex.GetType ()); return true;}); }; Work assignment 1 = new task (() => (New argument NullException ();}); Work task2 = new task (() => {new argument otofrange exception};};); Task1.Start (); Task2.Start (); While (! Task 1.Isim included !! Task 2.Ismplied) {Thread. Sleep (5000); } Console. "Line" ("done"); Console.ReadLine (); Unfortunately, that example will never show your code to you
Unfortunately, that example will never show your code. Unrestricted Task Exceptions will only be done if an action is collected with the exception of GC - unless you keep it in the context of task1 and task2 GC will never be collected, and you will never see exception handlers.
To see the behavior of unnecessary task exceptions , I will try the following (responsive example):
public static zero main () {Task scheduler. Unpublished Task Exception + = (Object Sender, Unaffected Task Exceptions Avent Agents Event) = & gt; {EventArgs.SetObserved (); ((Overall abstraction) eventArgs.Exception). Hondol (ex = & gt; {console. WrightLine ("exception type: {0}", ex.GetType ()); return true;}); }; Work.factor StartNew ((=) => New argument; Minimum Exception ();}); Work. Factory StartNew ((=) => Skip new logic; exception exception ();}); Thread.Sleep (100); GC.Collect (); GC.WaitForPendingFinalizers (); Console.WriteLine ("full"); Console.ReadKey (); } This will show you your messages. The first threads. Sleep (100) provides enough time to throw the call functions. Gather and wait for a GC collection, which will fire your event handler 2x.
Comments
Post a Comment