{"id":27275,"date":"2018-09-07T10:33:00","date_gmt":"2018-09-07T10:33:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/premier_developer\/?p=27275"},"modified":"2019-02-14T20:17:52","modified_gmt":"2019-02-15T03:17:52","slug":"raw-threads-and-async-lambdas","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/raw-threads-and-async-lambdas\/","title":{"rendered":"Raw threads and async lambdas"},"content":{"rendered":"<p>Senior Consultant, Ben Williams shares some suggestions on avoiding unexpected behaviors with async\/lambdas<\/p>\n<hr \/>\n<p>Using async methods\/lambdas where they are not expected causes unexpected problems. The typical example I discuss with people is <a href=\"https:\/\/blogs.msdn.microsoft.com\/pfxteam\/2012\/02\/08\/potential-pitfalls-to-avoid-when-passing-around-async-lambdas\/\">TaskFactory.StartNew()<\/a> because it\u2019s an easy way to create Tasks and some people reach for that instead of Task.Run(), but I recently came across some code hitting the same problem while creating threads the traditional way.<\/p>\n<p>In this scenario, the code was intended to create some child threads that poll a resource. That resource would sometimes be changed and the master thread would Abort() the child threads and create new ones polling the new resource. But there was a thread leak. Sometimes threads could continue to run after they should have been aborted.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/blogs.msdn.microsoft.com\/benwilli\/2018\/08\/27\/raw-threads-and-async-lambdas\/\">Read the full post on Ben\u2019s blog<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using async methods\/lambdas where they are not expected causes unexpected problems. The typical example I discuss with people is TaskFactory.StartNew() because its an easy way to create Tasks and some people reach for that instead of Task.Run(), but I recently came across some code hitting the same problem while creating threads the traditional way.<\/p>\n","protected":false},"author":582,"featured_media":27391,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[80],"tags":[106,148,3],"class_list":["post-27275","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net","tag-net","tag-asynchronous","tag-team"],"acf":[],"blog_post_summary":"<p>Using async methods\/lambdas where they are not expected causes unexpected problems. The typical example I discuss with people is TaskFactory.StartNew() because its an easy way to create Tasks and some people reach for that instead of Task.Run(), but I recently came across some code hitting the same problem while creating threads the traditional way.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/27275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/582"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=27275"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/27275\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/27391"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=27275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=27275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=27275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}