{"id":4778,"date":"2020-12-15T15:39:48","date_gmt":"2020-12-15T21:39:48","guid":{"rendered":"http:\/\/neosmart.net\/blog\/?p=4778"},"modified":"2020-12-15T15:39:48","modified_gmt":"2020-12-15T21:39:48","slug":"asynclock-3-0-for-net-5-0-released","status":"publish","type":"post","link":"https:\/\/neosmart.net\/blog\/asynclock-3-0-for-net-5-0-released\/","title":{"rendered":"AsyncLock 3.0 for .NET 5.0 Released"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/neosmart\/AsyncLock\" rel=\"nofollow\"><img loading=\"lazy\" decoding=\"async\" class=\"size-thumbnail wp-image-3393 colorbox-4778\" src=\"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/Threads-GitHub-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" style=\"float:right;\" srcset=\"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/Threads-GitHub-150x150.jpg 150w, https:\/\/neosmart.net\/blog\/wp-content\/uploads\/Threads-GitHub.jpg 200w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a>An updated version of <code>AsyncLock<\/code> has just been released on <a href=\"https:\/\/github.com\/neosmart\/asynclock\" rel=\"nofollow\">GitHub<\/a> and <a href=\"https:\/\/www.nuget.org\/packages\/NeoSmart.AsyncLock\/\" rel=\"follow\">Nuget<\/a> with improved support for reentrancy detection.<\/p>\n<p><code>AsyncLock<\/code> is a async\/await-native anonymous lock for .NET and .NET Core that allows safely using async\/await while holding a lock, for example:<\/p>\n<p><!--more--><\/p>\n<div style=\"clear: both;\">&nbsp;<\/div>\n<pre><code class=\"language-csharp\">class AsyncLockTest\r\n{\r\n    AsyncLock _lock = new AsyncLock();\r\n\r\n    void Test()\r\n    {\r\n        \/\/ The code below will be run immediately and asynchronously.\r\n        \/\/ It *may* be run in a new thread, but we can't know.\r\n        Task.Run(async () => {\r\n            \/\/ This first LockAsync() call should not block\r\n            using (await _lock.LockAsync())\r\n            {\r\n                \/\/ This second call to LockAsync() will be recognized\r\n                \/\/ as being a ree\u0308ntrant call and go through\r\n                using (await _lock.LockAsync())\r\n                {\r\n                    \/\/ We now hold the lock exclusively and no one else\r\n                    \/\/ can use it for 1 minute\r\n                    await Task.Delay(TimeSpan.FromMinutes(1));\r\n                }\r\n            }\r\n        }).Wait(TimeSpan.FromSeconds(30));\r\n\r\n        \/\/ This call to obtain the lock is synchronously made from the main\r\n        \/\/ thread. It will, however, block until the asynchronous code which\r\n        \/\/ obtained the lock above finishes\r\n        using (_lock.Lock())\r\n        {\r\n            \/\/ We have now obtained exclusive access\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>Refer to <a href=\"https:\/\/neosmart.net\/blog\/2017\/asynclock-an-asyncawait-friendly-locking-library-for-c-and-net\/\" rel=\"follow\">the original article covering async locking<\/a> for more information.<\/p>\n<p>You can install <code>AsyncLock<\/code> via the <code>dotnet<\/code> or <code>nuget<\/code> command line applications:<\/p>\n<pre>\r\ndotnet add reference NeoSmart.AsyncLock\r\n<\/pre>\n<div class=\"sendy_widget\" style='margin-bottom: 0.5em;'>\n<p><em>If you would like to receive a notification the next time we release a nuget package for .NET or release resources for .NET Core and ASP.NET Core, you can subscribe below. Note that you'll only get notifications relevant to .NET programming and development by NeoSmart Technologies. If you want to receive email updates for all NeoSmart Technologies posts and releases, please sign up in the sidebar to the right instead.<\/em><\/p>\n<iframe tabIndex=-1 onfocus=\"sendy_no_focus\" src=\"https:\/\/neosmart.net\/sendy\/subscription?f=BUopX8f2VyLSOb892VIx6W4BB8V5K2ReYGLVwsfKUZLXCc892Ffz8rIgRyIGoE22cZVr&title=Join+the+dotnet+mailing+list\" style=\"height: 300px; width: 100%;\"><\/iframe>\n<\/div>\n<script type=\"text\/javascript\">function sendy_no_focus(e) { e.preventDefault(); }<\/script>\n","protected":false},"excerpt":{"rendered":"<p>An updated version of AsyncLock has just been released on GitHub and Nuget with improved support for reentrancy detection. AsyncLock is a async\/await-native anonymous lock for .NET and .NET Core that allows safely using async\/await while holding a lock, for &hellip; <a href=\"https:\/\/neosmart.net\/blog\/asynclock-3-0-for-net-5-0-released\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":505,"featured_media":3389,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[999],"tags":[1009,325,212,759],"class_list":["post-4778","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-asynclock","tag-c","tag-development","tag-github"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/Threads.jpeg","jetpack_shortlink":"https:\/\/wp.me\/p4xDa-1f4","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/users\/505"}],"replies":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/comments?post=4778"}],"version-history":[{"count":6,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4778\/revisions"}],"predecessor-version":[{"id":4784,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4778\/revisions\/4784"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media\/3389"}],"wp:attachment":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media?parent=4778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/categories?post=4778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/tags?post=4778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}