Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   Performing a Simple Compute-Bound Operation

To queue an asynchronous compute-bound operation to the thread pool, you typically call one of the

following methods defined by the ThreadPool class.

 

static Boolean QueueUserWorkItem(WaitCallback callBack);

static Boolean QueueUserWorkItem(WaitCallback callBack, Object state);

 

These methods queue a “work item” and optional state data to the thread pool’s queue, and then all of these methods return immediately. A work item is simply a method identified by the callback parameter that will be called by a thread pool thread. The method can be passed a single parameter specified via the state (the state data) argument. The version of QueueUserWorkItem without the state parameter passes null to the callback method. Eventually, some thread in the pool will proc- ess the work item, causing your method to be called. The callback method you write must match the System.Threading.WaitCallback delegate type, which is defined as follows.

 

delegate void WaitCallback(Object state);

       
   
 
 

 

The following code demonstrates how to have a thread pool thread call a method asynchronously.

 

using System;

using System.Threading;

 

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

Console.WriteLine("Main thread: queuing an asynchronous operation"); ThreadPool.QueueUserWorkItem(ComputeBoundOp, 5); Console.WriteLine("Main thread: Doing other work here..."); Thread.Sleep(10000); // Simulating other work (10 seconds) Console.WriteLine("Hit <Enter> to end this program..."); Console.ReadLine();

}

 

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

// This method is executed by a thread pool thread


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

 

// When this method returns, the thread goes back

// to the pool and waits for another task

}

}

 

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

 

Main thread: queuing an asynchronous operation Main thread: Doing other work here...

In ComputeBoundOp: state=5

 

And, sometimes when I run this code, I get this output.

 

Main thread: queuing an asynchronous operation In ComputeBoundOp: state=5

Main thread: Doing other work here...

 

The difference in the order of the lines in the output is attributed to the fact that the two methods are running asynchronously with respect to one another. The Windows scheduler determines which thread to schedule first, or it may schedule them both simultaneously if the application is running on a multi-CPU machine.

       
   
 
 

 


Date: 2016-03-03; view: 763


<== previous page | next page ==>
Nbsp;   Introducing the CLR’s Thread Pool | Nbsp;   Execution Contexts
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.006 sec.)