Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   Using a Dedicated Thread to Perform an Asynchronous Compute-Bound Operation

In this section, I will show you how to create a thread and have it perform an asynchronous compute- bound operation. Although I am going to walk you through this, I highly recommend that you avoid the technique I show you here. And, in fact, this technique is not even possible if you are building a Windows Store app because the Thread class is not available. Instead, you should use the thread pool


to execute asynchronous compute-bound operations whenever possible. I go into the details about

doing this in Chapter 27, “Compute-Bound Asynchronous Operations.”

 

However, there are some very unusual occasions when you might want to explicitly create a thread dedicated to executing a particular compute-bound operation. Typically, you’d want to create a dedi- cated thread if you’re going to execute code that requires the thread to be in a particular state that

is not normal for a thread pool thread. For example, explicitly create your own thread if any of the following is true:

■ You need the thread to run with a non-normal thread priority. All thread pool threads run at normal priority. Although you can change this, it is not recommended, and the priority change does not persist across thread pool operations.

■ You need the thread to behave as a foreground thread, thereby preventing the application from dying until the thread has completed its task. For more information, see the ”Foreground Threads vs. Background Threads” section later in this chapter. Thread pool threads are always background threads, and they may not complete their task if the CLR wants to terminate the process.

■ The compute-bound task is extremely long-running; this way, I would not be taxing the thread

pool’s logic as it tries to figure out whether to create an additional thread.

 

■ You want to start a thread and possibly abort it prematurely by calling Thread’s Abort

method (discussed in Chapter 22, “CLR Hosting and AppDomains”).

 

To create a dedicated thread, you construct an instance of the System.Threading.Thread class, passing the name of a method into its constructor. Here is the prototype of Thread’s constructor.

 

public sealed class Thread : CriticalFinalizerObject, ... { public Thread(ParameterizedThreadStart start);

// Less commonly used constructors are not shown here

}

 

The start parameter identifies the method that the dedicated thread will execute, and this

method must match the signature of the ParameterizedThreadStart delegate.4

 

delegate void ParameterizedThreadStart(Object obj);

 

Constructing a Thread object is a relatively lightweight operation because it does not actually create a physical operating system thread. To actually create the operating system thread and have it start executing the callback method, you must call Thread’s Start method, passing into it the object (state) that you want passed as the callback method’s argument.

 

 
 



4 For the record, Thread also offers a constructor that takes a ThreadStart delegate that accepts no arguments and returns void. Personally, I recommend that you avoid this constructor and delegate because they are more limiting. If your thread method takes an Object and returns void, then you can invoke your method by using a dedicated thread or invoke it by using the thread pool (as shown in Chapter 27).


The following code demonstrates how to create a dedicated thread and have it call a method asynchronously.

 

using System;

using System.Threading;

 

public static class Program { public static void Main() {

Console.WriteLine("Main thread: starting a dedicated thread " + "to do an asynchronous operation");

Thread dedicatedThread = new Thread(ComputeBoundOp); dedicatedThread.Start(5);

 

Console.WriteLine("Main thread: Doing other work here..."); Thread.Sleep(10000); // Simulating other work (10 seconds)

 

dedicatedThread.Join(); // Wait for thread to terminate Console.WriteLine("Hit <Enter> to end this program..."); Console.ReadLine();

}

 

// This method's signature must match the ParameterizedThreadStart delegate private static void ComputeBoundOp(Object state) {

// This method is executed by a dedicated thread

 

Console.WriteLine("In ComputeBoundOp: state={0}", state); Thread.Sleep(1000); // Simulates other work (1 second)

 

// When this method returns, the dedicated thread dies

}

}

 

When I compile and run this code, I get the following output.

 

Main thread: starting a dedicated thread to do an asynchronous operation Main thread: Doing other work here...

In ComputeBoundOp: state=5

 

Sometimes when I run this code, I get the following output, because I can’t control how Windows schedules the two threads.

 

Main thread: starting a dedicated thread to do an asynchronous operation In ComputeBoundOp: state=5

Main thread: Doing other work here...

 

Notice that the Main method calls Join. The Join method causes the calling thread to stop executing any code until the thread identified by dedicatedThread has destroyed itself or been terminated.



Date: 2016-03-03; view: 823


<== previous page | next page ==>
Nbsp;   CPU Trends | Nbsp;   Reasons to Use Threads
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.008 sec.)