{"id":8134,"date":"2020-04-30T18:37:46","date_gmt":"2020-04-30T18:37:46","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8134"},"modified":"2020-04-30T18:37:46","modified_gmt":"2020-04-30T18:37:46","slug":"net-channels","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/net-channels\/","title":{"rendered":".NET Channels"},"content":{"rendered":"<p>This post is based upon a Channel9 video with Stephen Toub, I recommend you go and watch it (see references) if you&#8217;re interested in threadsafe producer\/consumer mechanisms.<\/p>\n<p>This is the namespace you want to be looking at<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System.Threading.Channels;\r\n<\/pre>\n<p>There are two types of channel, bounded and unbounded. <\/p>\n<p>Bounded channels have a fixed size. In this example the queue within the channel will only allow a maximum of 10 items, meaning if we try to write more than the 10 items, the write method would block until an item is removed\/read from the queue<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar c = Channel.CreateBounded&lt;int&gt;(10);\r\n<\/pre>\n<p>So from this, we can tell that unbounded means the queue within the channel in unlimited in size. We create an unbounded channel like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar c = Channel.CreateUnbounded&lt;int&gt;();\r\n<\/pre>\n<p>When we want to add items to the channel we use code such as the following, obviously if the queue has reached a limit (bounded) then this will block<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nc.Writer.TryWrite(123);\r\n<\/pre>\n<p>To read a value from the channel (if one exists) we use<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nawait c.Reader.ReadAsync()\r\n<\/pre>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/channel9.msdn.com\/Shows\/On-NET\/Working-with-Channels-in-NET\" rel=\"noopener noreferrer\" target=\"_blank\">Working with Channels in .NET<\/a><br \/>\n<a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/an-introduction-to-system-threading-channels\/\" rel=\"noopener noreferrer\" target=\"_blank\">An Introduction to System.Threading.Channels<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is based upon a Channel9 video with Stephen Toub, I recommend you go and watch it (see references) if you&#8217;re interested in threadsafe producer\/consumer mechanisms. This is the namespace you want to be looking at using System.Threading.Channels; There are two types of channel, bounded and unbounded. Bounded channels have a fixed size. In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[49,3,95],"tags":[],"class_list":["post-8134","post","type-post","status-publish","format-standard","hentry","category-net","category-c","category-multithreading"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8134"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8134\/revisions"}],"predecessor-version":[{"id":8155,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8134\/revisions\/8155"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}