Posting an asynchronous operation on a thread pool
This recipe will describe how to put an asynchronous operation on a thread pool.
Getting ready
To step into this recipe, you will need Visual Studio 2012. There are no other prerequisites. The source code for this recipe could be found in BookSamples\Chapter3\Recipe2
.
How to do it...
To understand how to post an asynchronous operation on a thread pool, perform the following steps:
- Start Visual Studio 2012. Create a new C# Console Application project.
- In the
Program.cs
file, add the followingusing
directives:using System; usingSystem.Threading;
- Add the following code snippet below the
Main
method:private static void AsyncOperation(object state) { Console.WriteLine("Operation state: {0}",state ?? "(null)"); Console.WriteLine("Worker thread id: {0}",Thread.CurrentThread.ManagedThreadId); Thread.Sleep(TimeSpan.FromSeconds(2)); }
- Add the following code snippet inside the
Main
method:const int x = 1; const int y ...