{"id":186,"date":"2023-04-05T18:49:33","date_gmt":"2023-04-05T18:49:33","guid":{"rendered":"https:\/\/www.w3computing.com\/articles\/?p=186"},"modified":"2023-08-23T16:22:31","modified_gmt":"2023-08-23T16:22:31","slug":"implementing-parallel-and-asynchronous-programming-with-c","status":"publish","type":"post","link":"https:\/\/www.w3computing.com\/articles\/implementing-parallel-and-asynchronous-programming-with-c\/","title":{"rendered":"Implementing Parallel and Asynchronous Programming with C#"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The continuous growth in computer hardware capabilities has led to a steady increase in the number of applications that rely on parallel and asynchronous programming techniques. One of the most popular programming languages for modern software development is C#. You may already be familiar with the basics of C#, but implementing parallel and asynchronous programming can help you take your projects to the next level.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this comprehensive guide, we&#8217;ll explore the benefits of parallel and asynchronous programming, dive into the various tools and techniques that C# provides, and provide practical examples to enhance your understanding of these concepts. This guide is aimed at intermediate developers, but even if you&#8217;re a seasoned C# programmer, you may find new insights and techniques to improve your code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1: Parallel Programming in C#<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 Understanding Parallelism<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Parallelism is the process of executing multiple tasks or threads simultaneously, allowing your application to perform tasks more efficiently. Parallel programming techniques can help you to take full advantage of multi-core processors, which are now commonplace in modern computers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.2 The Task Parallel Library (TPL)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">C# offers the Task Parallel Library (TPL), a set of classes and methods designed to make it easier to create, run, and manage parallel tasks. The TPL is built on top of the ThreadPool, which manages a pool of worker threads for executing tasks. The TPL abstracts away many of the complexities of working with threads directly, allowing you to focus on implementing parallel algorithms.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.3 <code>Parallel.ForEach<\/code> and <code>Parallel.For<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong><code>Parallel.ForEach<\/code><\/strong> and <code><strong>Parallel.For<\/strong><\/code> methods are two of the most commonly used TPL methods. These methods allow you to perform a parallel loop, dividing the iterations of the loop among available cores. This can greatly improve the performance of CPU-bound tasks, such as processing large data sets or performing complex calculations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.4 Handling Exceptions in Parallel Code<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">One of the challenges of parallel programming is handling exceptions that occur within parallel tasks. The TPL provides the AggregateException class, which captures multiple exceptions that occur during parallel execution. By using try-catch blocks and the AggregateException class, you can ensure that your parallel code is robust and handles errors gracefully.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2: Asynchronous Programming with C#<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 Understanding Asynchrony<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Asynchronous programming is a technique that allows you to perform time-consuming operations without blocking the execution of your application. This is particularly useful when dealing with I\/O-bound operations, such as reading from a file or downloading data from the internet.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 The <code>async<\/code> and <code>await<\/code> Keywords<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">C# introduced the <code><strong>async<\/strong><\/code> and <code><strong>await<\/strong><\/code> keywords in version 5.0 to simplify the process of writing asynchronous code. By marking a method with the <strong><code>async<\/code><\/strong> keyword, you indicate that it will contain one or more await expressions. These expressions allow the method to yield control back to the caller, while the awaited operation is performed. This prevents the application from becoming unresponsive during lengthy operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.3 Task-based Asynchronous Pattern (TAP)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Task-based Asynchronous Pattern (TAP) is a recommended approach to asynchronous programming in C#. TAP relies on the Task and Task classes to represent ongoing work that may not have completed yet. By using TAP, you can write asynchronous code that is easier to read, maintain, and debug.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.4 Combining Asynchronous and Parallel Programming<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In some scenarios, you may need to combine parallel and asynchronous programming to achieve optimal performance. C# provides various tools and techniques to help you combine these approaches effectively. For example, you can use the Task.WhenAll method to await the completion of multiple asynchronous tasks in parallel.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3: Practical Examples and Tips<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 Example: Parallelizing a CPU-bound Operation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Consider a scenario where you need to process a large collection of data points and perform complex calculations on each of them. The following example demonstrates how to use the <strong><code>Parallel.ForEach<\/code><\/strong> method to parallelize this CPU-bound operation:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">List&lt;DataPoint&gt; dataPoints = GetDataPoints();\r\nList&lt;Result&gt; results = <span class=\"hljs-keyword\">new<\/span> List&lt;Result&gt;();\r\n\r\nParallel.ForEach(dataPoints, dataPoint =&gt;\r\n{\r\n    Result result = PerformComplexCalculation(dataPoint);\r\n    <span class=\"hljs-keyword\">lock<\/span> (results)\r\n    {\r\n        results.Add(result);\r\n    }\r\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, the <strong><code>Parallel.ForEach<\/code><\/strong> method automatically distributes the workload across multiple cores, improving the overall performance of the operation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 Example: Asynchronous File I\/O<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When working with file I\/O operations, asynchronous programming can help prevent your application from becoming unresponsive. The following example demonstrates how to read the contents of a file asynchronously using the <code><strong>StreamReader<\/strong><\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">async<\/span> Task&lt;<span class=\"hljs-keyword\">string<\/span>&gt; <span class=\"hljs-title\">ReadFileAsync<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> filePath<\/span>)<\/span>\r\n{\r\n    <span class=\"hljs-keyword\">using<\/span> (StreamReader reader = <span class=\"hljs-keyword\">new<\/span> StreamReader(filePath))\r\n    {\r\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">await<\/span> reader.ReadToEndAsync();\r\n    }\r\n}\r\n\r\n<span class=\"hljs-comment\">\/\/ Usage<\/span>\r\n<span class=\"hljs-keyword\">string<\/span> filePath = <span class=\"hljs-string\">\"example.txt\"<\/span>;\r\n<span class=\"hljs-keyword\">string<\/span> fileContents = <span class=\"hljs-keyword\">await<\/span> ReadFileAsync(filePath);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">By using the async and await keywords, you can perform file I\/O operations without blocking the main thread of your application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 Tips for Writing Efficient Parallel and Asynchronous Code<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Choose the right technique<\/strong>: Evaluate whether your operation is CPU-bound or I\/O-bound, and choose parallel or asynchronous programming accordingly.<\/li>\n\n\n\n<li><strong>Use cancellation tokens<\/strong>: Pass CancellationToken instances to your parallel or asynchronous tasks to allow for graceful cancellation of ongoing operations.<\/li>\n\n\n\n<li><strong>Limit the degree of parallelism<\/strong>: When using TPL methods, consider setting the MaxDegreeOfParallelism property to prevent oversubscription of resources.<\/li>\n\n\n\n<li><strong>Handle exceptions properly<\/strong>: Ensure that you use try-catch blocks and the AggregateException class to handle exceptions in parallel and asynchronous code.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example Exercise: Parallel Image Processing and Asynchronous Download<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this exercise, we&#8217;ll create a C# console application that downloads a set of images from the internet asynchronously and then processes them in parallel to apply a grayscale filter. We will be using the TPL and async-await features discussed in the previous sections.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create a new C# Console Application project<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a new C# Console Application project in Visual Studio or your favorite C# development environment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Add the necessary NuGet packages<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We will be using the SixLabors.ImageSharp library for image processing. Add the following NuGet package to your project:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>SixLabors.ImageSharp<\/code><\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">To add the NuGet package in Visual Studio, right-click on your project in the Solution Explorer and select &#8220;Manage NuGet Packages.&#8221; Click on the &#8220;Browse&#8221; tab, search for <strong><code>SixLabors.ImageSharp<\/code><\/strong>, select it from the search results, and click &#8220;Install&#8221; to add it to your project.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Visual Studio Code, you can use the terminal to add the package by running the following command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">dotnet <span class=\"hljs-keyword\">add<\/span> package SixLabors.ImageSharp\r<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Step 3: Implement the image download and processing methods<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following methods to your <strong><code>Program<\/code><\/strong> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">using<\/span> System;\r\n<span class=\"hljs-keyword\">using<\/span> System.Collections.Generic;\r\n<span class=\"hljs-keyword\">using<\/span> System.IO;\r\n<span class=\"hljs-keyword\">using<\/span> System.Net.Http;\r\n<span class=\"hljs-keyword\">using<\/span> System.Threading.Tasks;\r\n<span class=\"hljs-keyword\">using<\/span> SixLabors.ImageSharp;\r\n<span class=\"hljs-keyword\">using<\/span> SixLabors.ImageSharp.PixelFormats;\r\n<span class=\"hljs-keyword\">using<\/span> SixLabors.ImageSharp.Processing;\r\n\r\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">async<\/span> Task <span class=\"hljs-title\">DownloadImageAsync<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> url, <span class=\"hljs-keyword\">string<\/span> destination<\/span>)<\/span>\r\n{\r\n    <span class=\"hljs-keyword\">using<\/span> HttpClient client = <span class=\"hljs-keyword\">new<\/span> HttpClient();\r\n    <span class=\"hljs-keyword\">using<\/span> HttpResponseMessage response = <span class=\"hljs-keyword\">await<\/span> client.GetAsync(url);\r\n    response.EnsureSuccessStatusCode();\r\n\r\n    <span class=\"hljs-keyword\">using<\/span> Stream responseStream = <span class=\"hljs-keyword\">await<\/span> response.Content.ReadAsStreamAsync();\r\n    <span class=\"hljs-keyword\">using<\/span> FileStream fileStream = File.Create(destination);\r\n\r\n    <span class=\"hljs-keyword\">await<\/span> responseStream.CopyToAsync(fileStream);\r\n}\r\n\r\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">ProcessImageInParallel<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> sourcePath, <span class=\"hljs-keyword\">string<\/span> destinationPath<\/span>)<\/span>\r\n{\r\n    <span class=\"hljs-keyword\">using<\/span> Image&lt;Rgba32&gt; image = Image.Load&lt;Rgba32&gt;(sourcePath);\r\n    Parallel.For(<span class=\"hljs-number\">0<\/span>, image.Height, y =&gt;\r\n    {\r\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> x = <span class=\"hljs-number\">0<\/span>; x &lt; image.Width; x++)\r\n        {\r\n            Rgba32 pixel = image&#91;x, y];\r\n            <span class=\"hljs-keyword\">byte<\/span> gray = (<span class=\"hljs-keyword\">byte<\/span>)((<span class=\"hljs-number\">0.3<\/span> * pixel.R) + (<span class=\"hljs-number\">0.59<\/span> * pixel.G) + (<span class=\"hljs-number\">0.11<\/span> * pixel.B));\r\n            pixel.R = pixel.G = pixel.B = gray;\r\n            image&#91;x, y] = pixel;\r\n        }\r\n    });\r\n\r\n    image.Save(destinationPath);\r\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The <strong><code>DownloadImageAsync<\/code><\/strong> method takes a URL and a destination file path, downloads the image asynchronously, and saves it to the destination path. The <strong><code>ProcessImageInParallel<\/code><\/strong> method takes a source image path and a destination path, loads the image, processes it in parallel using a grayscale filter, and then saves the processed image to the destination path.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Implement the main application logic<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following code to your <strong><code>Main<\/code><\/strong> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">async<\/span> Task <span class=\"hljs-title\">Main<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] args<\/span>)<\/span>\r\n{\r\n    List&lt;<span class=\"hljs-keyword\">string<\/span>&gt; imageUrls = <span class=\"hljs-keyword\">new<\/span> List&lt;<span class=\"hljs-keyword\">string<\/span>&gt;\r\n    {\r\n        <span class=\"hljs-string\">\"https:\/\/example.com\/image1.jpg\"<\/span>,\r\n        <span class=\"hljs-string\">\"https:\/\/example.com\/image2.jpg\"<\/span>,\r\n        <span class=\"hljs-comment\">\/\/ Add more image URLs<\/span>\r\n    };\r\n\r\n    <span class=\"hljs-keyword\">int<\/span> counter = <span class=\"hljs-number\">0<\/span>;\r\n    List&lt;Task&gt; downloadTasks = <span class=\"hljs-keyword\">new<\/span> List&lt;Task&gt;();\r\n    <span class=\"hljs-keyword\">foreach<\/span> (<span class=\"hljs-keyword\">string<\/span> url <span class=\"hljs-keyword\">in<\/span> imageUrls)\r\n    {\r\n        <span class=\"hljs-keyword\">string<\/span> destination = <span class=\"hljs-string\">$\"image<span class=\"hljs-subst\">{++counter}<\/span>.jpg\"<\/span>;\r\n        downloadTasks.Add(DownloadImageAsync(url, destination));\r\n    }\r\n\r\n    <span class=\"hljs-comment\">\/\/ Wait for all images to be downloaded<\/span>\r\n    <span class=\"hljs-keyword\">await<\/span> Task.WhenAll(downloadTasks);\r\n\r\n    List&lt;Task&gt; processingTasks = <span class=\"hljs-keyword\">new<\/span> List&lt;Task&gt;();\r\n    <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> i = <span class=\"hljs-number\">1<\/span>; i &lt;= counter; i++)\r\n    {\r\n        <span class=\"hljs-keyword\">string<\/span> sourcePath = <span class=\"hljs-string\">$\"image<span class=\"hljs-subst\">{i}<\/span>.jpg\"<\/span>;\r\n        <span class=\"hljs-keyword\">string<\/span> destinationPath = <span class=\"hljs-string\">$\"image<span class=\"hljs-subst\">{i}<\/span>_grayscale.jpg\"<\/span>;\r\n        processingTasks.Add(Task.Run(() =&gt; ProcessImageInParallel(sourcePath, destinationPath)));\r\n    }\r\n\r\n    <span class=\"hljs-comment\">\/\/ Wait for all images to be processed<\/span>\r\n    <span class=\"hljs-keyword\">await<\/span> Task.WhenAll(processingTasks);\r\n\r\n    Console.WriteLine(<span class=\"hljs-string\">\"All images downloaded and processed successfully!\"<\/span>);\r\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This code initializes a list of image URLs, downloads them asynchronously, and processes them in parallel to apply a grayscale filter. After downloading and processing all images, it prints a success message.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, we iterate through the list of image URLs and call the <strong><code>DownloadImageAsync<\/code><\/strong> method for each URL, adding the resulting tasks to a <strong><code>downloadTasks<\/code><\/strong> list. Then, we use <strong><code>Task.WhenAll<\/code><\/strong> to wait for all download tasks to complete.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After downloading all the images, we loop through the downloaded images and call the <strong><code>ProcessImageInParallel<\/code><\/strong> method for each image. We wrap this method call inside a <strong><code>Task.Run<\/code><\/strong> to execute the processing in parallel and add the resulting tasks to a <strong><code>processingTasks<\/code><\/strong> list. Finally, we use <strong><code>Task.WhenAll<\/code><\/strong> to wait for all processing tasks to complete.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Test the application<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To test the application, replace the example URLs in the <strong><code>imageUrls<\/code><\/strong> list with valid image URLs. Run the application, and observe that it downloads and processes the images asynchronously and in parallel, creating new grayscale images in the application&#8217;s working directory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This exercise demonstrates how to combine parallel and asynchronous programming techniques in C# to download and process images efficiently. By using the TPL and async-await features, you can create high-performance applications that take full advantage of modern hardware and provide a responsive user experience.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The continuous growth in computer hardware capabilities has led to a steady increase in the number of applications that rely on parallel and asynchronous programming techniques. One of the most popular programming languages for modern software development is C#. You may already be familiar with the basics of C#, but implementing parallel and asynchronous programming [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[8,4],"tags":[],"class_list":["post-186","post","type-post","status-publish","format-standard","category-csharp","category-programming-languages","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Implementing Parallel and Asynchronous Programming with C#<\/title>\n<meta name=\"description\" content=\"Explore the benefits of parallel and asynchronous programming, dive into the various tools and techniques that C# provides, and provide\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.w3computing.com\/articles\/implementing-parallel-and-asynchronous-programming-with-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Implementing Parallel and Asynchronous Programming with C#\" \/>\n<meta property=\"og:description\" content=\"Explore the benefits of parallel and asynchronous programming, dive into the various tools and techniques that C# provides, and provide\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.w3computing.com\/articles\/implementing-parallel-and-asynchronous-programming-with-c\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-05T18:49:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-23T16:22:31+00:00\" \/>\n<meta name=\"author\" content=\"w3compadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"w3compadmin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-parallel-and-asynchronous-programming-with-c\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-parallel-and-asynchronous-programming-with-c\\\/\"},\"author\":{\"name\":\"w3compadmin\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"headline\":\"Implementing Parallel and Asynchronous Programming with C#\",\"datePublished\":\"2023-04-05T18:49:33+00:00\",\"dateModified\":\"2023-08-23T16:22:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-parallel-and-asynchronous-programming-with-c\\\/\"},\"wordCount\":1232,\"commentCount\":0,\"articleSection\":[\"C#\",\"Programming Languages\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-parallel-and-asynchronous-programming-with-c\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-parallel-and-asynchronous-programming-with-c\\\/\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-parallel-and-asynchronous-programming-with-c\\\/\",\"name\":\"Implementing Parallel and Asynchronous Programming with C#\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\"},\"datePublished\":\"2023-04-05T18:49:33+00:00\",\"dateModified\":\"2023-08-23T16:22:31+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"description\":\"Explore the benefits of parallel and asynchronous programming, dive into the various tools and techniques that C# provides, and provide\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-parallel-and-asynchronous-programming-with-c\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-parallel-and-asynchronous-programming-with-c\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-parallel-and-asynchronous-programming-with-c\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Articles Home\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programming Languages\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/programming-languages\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"C#\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/programming-languages\\\/csharp\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Implementing Parallel and Asynchronous Programming with C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\",\"name\":\"Developer Articles Hub\",\"description\":\"\",\"alternateName\":\"Developer Articles\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\",\"name\":\"w3compadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"contentUrl\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"caption\":\"w3compadmin\"},\"sameAs\":[\"http:\\\/\\\/w3computing.com\\\/articles\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Implementing Parallel and Asynchronous Programming with C#","description":"Explore the benefits of parallel and asynchronous programming, dive into the various tools and techniques that C# provides, and provide","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.w3computing.com\/articles\/implementing-parallel-and-asynchronous-programming-with-c\/","og_locale":"en_US","og_type":"article","og_title":"Implementing Parallel and Asynchronous Programming with C#","og_description":"Explore the benefits of parallel and asynchronous programming, dive into the various tools and techniques that C# provides, and provide","og_url":"https:\/\/www.w3computing.com\/articles\/implementing-parallel-and-asynchronous-programming-with-c\/","article_published_time":"2023-04-05T18:49:33+00:00","article_modified_time":"2023-08-23T16:22:31+00:00","author":"w3compadmin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"w3compadmin","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/www.w3computing.com\/articles\/implementing-parallel-and-asynchronous-programming-with-c\/#article","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/implementing-parallel-and-asynchronous-programming-with-c\/"},"author":{"name":"w3compadmin","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"headline":"Implementing Parallel and Asynchronous Programming with C#","datePublished":"2023-04-05T18:49:33+00:00","dateModified":"2023-08-23T16:22:31+00:00","mainEntityOfPage":{"@id":"https:\/\/www.w3computing.com\/articles\/implementing-parallel-and-asynchronous-programming-with-c\/"},"wordCount":1232,"commentCount":0,"articleSection":["C#","Programming Languages"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.w3computing.com\/articles\/implementing-parallel-and-asynchronous-programming-with-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.w3computing.com\/articles\/implementing-parallel-and-asynchronous-programming-with-c\/","url":"https:\/\/www.w3computing.com\/articles\/implementing-parallel-and-asynchronous-programming-with-c\/","name":"Implementing Parallel and Asynchronous Programming with C#","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/#website"},"datePublished":"2023-04-05T18:49:33+00:00","dateModified":"2023-08-23T16:22:31+00:00","author":{"@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"description":"Explore the benefits of parallel and asynchronous programming, dive into the various tools and techniques that C# provides, and provide","breadcrumb":{"@id":"https:\/\/www.w3computing.com\/articles\/implementing-parallel-and-asynchronous-programming-with-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.w3computing.com\/articles\/implementing-parallel-and-asynchronous-programming-with-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.w3computing.com\/articles\/implementing-parallel-and-asynchronous-programming-with-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Articles Home","item":"https:\/\/www.w3computing.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Programming Languages","item":"https:\/\/www.w3computing.com\/articles\/programming-languages\/"},{"@type":"ListItem","position":3,"name":"C#","item":"https:\/\/www.w3computing.com\/articles\/programming-languages\/csharp\/"},{"@type":"ListItem","position":4,"name":"Implementing Parallel and Asynchronous Programming with C#"}]},{"@type":"WebSite","@id":"https:\/\/www.w3computing.com\/articles\/#website","url":"https:\/\/www.w3computing.com\/articles\/","name":"Developer Articles Hub","description":"","alternateName":"Developer Articles","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.w3computing.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561","name":"w3compadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","url":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","contentUrl":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","caption":"w3compadmin"},"sameAs":["http:\/\/w3computing.com\/articles"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"w3compadmin","author_link":"https:\/\/www.w3computing.com\/articles\/author\/w3compadmin\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/comments?post=186"}],"version-history":[{"count":3,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":189,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/186\/revisions\/189"}],"wp:attachment":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/media?parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/categories?post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/tags?post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}