background preloader

Threading

Facebook Twitter

Task Schedulers. The following example shows some tasks that are scheduled on the global queue and other tasks that are scheduled on the local queue. void QueueTasks() { // TaskA is a top level task.

Task Schedulers

Task taskA = Task.Factory.StartNew( () => { Console.WriteLine("I was enqueued on the thread pool's global queue. "); // TaskB is a nested task and TaskC is a child task. Both go to local queue. Task taskB = new Task( ()=> Console.WriteLine("I was enqueued on the local queue. ")); Task taskC = new Task(() => Console.WriteLine("I was enqueued on the local queue, too.

"), TaskCreationOptions.AttachedToParent); taskB.Start(); taskC.Start(); }); } In some cases, when a Task is waited on, it may be executed synchronously on the Thread that is performing the wait operation. Asynchronous Programming in .Net: Async and Await for Beginners. Introduction There are several ways of doing asynchronous programming in .Net.

Asynchronous Programming in .Net: Async and Await for Beginners

Visual Studio 2012 introduces a new approach using the ‘await’ and ‘async’ keywords. These tell the compiler to construct task continuations in quite an unusual way. I found them quite difficult to understand using the Microsoft documentation, which annoyingly keeps saying how easy they are. This series of articles is intended to give a quick recap of some previous approaches to asynchronous programming to give us some context, and then to give a quick and hopefully easy introduction to the new keywords Example By far the easiest way to get to grips with the new keywords is by seeing an example.

Since this article is about asynchronous programming we will want the long-running method to run asynchronously on a background thread. In the real world the method could be running a report, or calling a web service. Private string LongRunningMethod(string message) { Thread.Sleep(2000); return "Hello " + message; } Code. Threading in C# - Free E-book. Threading in C# Joseph Albahari Last updated: 2011-4-27 Translations: Chinese | Czech | Persian | Russian | Japanese Download PDF Part 1: Getting Started C# supports parallel execution of code through multithreading.

Threading in C# - Free E-book

A C# client program (Console, WPF, or Windows Forms) starts in a single thread created automatically by the CLR and operating system (the “main” thread), and is made multithreaded by creating additional threads. All examples assume the following namespaces are imported: using System; using System.Threading; class ThreadTest{ static void Main() { Thread t = new Thread (WriteY); t.Start(); for (int i = 0; i < 1000; i++) Console.Write ("x"); } static void WriteY() { for (int i = 0; i < 1000; i++) Console.Write ("y"); }} The main thread creates a new thread t on which it runs a method that repeatedly prints the character “y”.

Once started, a thread’s IsAlive property returns true, until the point where the thread ends. Done static void Go(){ if (! Done Done (usually!) Join and Sleep. .NET 4.0 and System.Threading.Tasks. In the soon-to-be-released .NET 4.0 framework and Visual Studio 2010 we are going to get a plethora of new tools to help us write better multi-threaded applications.

.NET 4.0 and System.Threading.Tasks

One of these tools is a new namespace within the System.Threading namespace which is called "Tasks". The Tasks in System.Threading.Tasks namespace are a method of fine grained parallelism, similar to creating and using threads, but they have a few key differences. The main difference is that Tasks in .NET 4.0 don’t actually correlate to a new thread, they are executed on the new thread pool that is being shipped in .NET 4.0. So, creating a new task is similar to what we did in .NET 2.0 when we said: Okay, so if all we are doing is just plopping a new task on the thread pool, then why do we need this new Task namespace? In order to wait, we could have done something like this: But that is just a tad bit ugly. Creating Tasks Let’s look at how we could create one of these tasks: This way we now have a reference to the task.

Summary.