Clr_via_c #. 3rd translation [25.7 use a dedicated thread to execute asynchronous computing restrictions]

Source: Internet
Author: User

25.7 using a dedicated thread to perform an asynchronous compute-bound operation

Use a dedicated thread to execute asynchronous computing restrictions

 

This section describes how to create a thread and perform an asynchronous compute-bound operation. Although it will teach you how to do it, we strongly recommend that you avoid using the technology shown here. On the contrary,We recommend that you use the CLR thread pool to execute asynchronous computing restrictions.In Chapter 26th"Asynchronous operations restricted by computing"Discussion.

 

In some cases, you may want to explicitly create a thread to specifically execute a computing restriction operation. IfCodeA thread is required to be in a specific State, which is unusual for the thread pool thread, so you can consider creating a dedicated thread. For example, you can explicitly create your own thread if any of the following conditions are met:

The thread must run at a non-normal thread priority.. All threads in the thread pool run with a normal priority. Although this priority can be changed, it is not recommended to do that. In addition, priority changes cannot be sustained between different thread pool operations.

The front-end thread is required to avoid application.ProgramTerminate before the thread ends its task.. For more information, see the "foreground thread and background thread" section in this chapter. The thread pool thread is always a background thread. IfCLRTo terminate processes, they may be forced to complete tasks.

Tasks restricted by computing need to run for a long timeThe logic used by the thread pool is complicated to determine whether an additional thread needs to be created. You can avoid this problem by directly creating a dedicated thread for a long-running task.

To start a thread and possibly callThreadOfAbortMethod to terminate it in advance(IN22Chapter"CLR boardingAnd application domain)

 

To create a dedicated thread, You need to constructSystem. Threading. ThreadClass to pass the name of a method to its constructor. Below isThreadConstructor prototype:

 

 
Public sealed class thread: criticalfinalizerobject,... {public thread (parameterizedthreadstart start); // less commonly used constructors are not shown here}

The start parameter identifies the method to be executed by a dedicated thread. This method must bePrameterizedthreadstartThe signature of the delegate.

 
Delegate void parameterizedthreadstart (Object OBJ );

construct thread an object is a relatively lightweight operation because it does not actually create an operating system thread. You must call thread Start the method that transmits the object (State) to be passed as the real parameter of the callback method ). The Code demonstrates how to create a dedicated thread and make 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 delegateprivate 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 }}

Run the code to get the following output:

Main thread: starting a dedicated thread to do an asynchronous operationmain thread: doing other work here... In computeboundop: State = 5

Sometimes, running the code will get the following output, because we cannot controlWindowsHow to schedule these two threads.

 
Main thread: starting a dedicated thread to do an asynchronous Operationin computeboundop: State = 5 main thread: doing other work here...

Note,MainMethod calledJion.JoinMethod causes the call thread to block any code currently executedDedicatedthreadIndicates that the thread is destroyed or terminated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.