Strange things have happened again recently, IIS application pool did not hang out, all point to the same spear, Async,threadpool,task, There is also a system.nullreferenceexception, so these all make us feel that our async program is having problems, and the fact is that our asynchronous invocation references a non-null reference to the "context" and eventually causes the w3wp process to die!
Through other senior share, found the cause of the problem, the uncle also summed up
1 The Async method needs to wait for its result with await, which guarantees that your SynchronizationContext context is not empty, that is, there is no error with a non-null reference.
2 when calling the Async method, you can also use its configureawait (false) method if no method is added to the await keyword,although it does not save the SynchronizationContext context. However, it does not report a non-null reference error.
3 Call Async Async method in a new thread, we need to pay attention to the above two points
See article
Http://www.cnblogs.com/cmt/p/configure_await_false.html
Http://www.cnblogs.com/cmt/p/sokcet_memory_leak.html
Technical point Description
1 Task.run (() =>{}); Add a task to the thread pool, queue up for execution
2 Async identifies a method as an async method that can be executed in parallel with the main thread, exerting the multi-core advantage of the CPU
3 await you can add this modifier before calling an async method, which means to wait for the current async method to execute and then execute the following code
4 Configureawait (True), when the code is executed by synchronous execution into asynchronous execution, the current thread context information is captured and saved to SynchronizationContext for use in asynchronous execution, and is used in synchronous execution after completion of asynchronous execution
5 configurewait (flase), without capturing the thread context information, the Async method cannot get the context information of the thread before the await when it executes with the code after await. The most direct effect in ASP. Httpconext.current is null, but there is no non-null reference error
The above is what we learned in solving the w3wp.exe crashes caused by asynchrony!
Thank you for reading!
c#~ Asynchronous Programming ~await and Async-induced w3wp.exe crashes