Threadpool's queueuserworkitem method (waitcallback) and queueuserworkitem method (waitcallback, object)

Source: Internet
Author: User

Note: to use this method, you must introduce the application: using system. Threading; // introduce the application.

It indicates that the method to be executed is queued for execution,WaitcallbackIndicates the method to be executed;ObjectThe object that contains the data used by the method. If the method is successfully queuedTrueOtherwiseFalse.

1. The following is the sample code of the threadpool. queueuserworkitem method (waitcallback:

1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. text; 5 using system. threading; // introduce application 6 7 namespace queueuserworkitem_waitcallback _ 8 {9 class program10 {11 static void main (string [] ARGs) 12 {13 // queue the task.14 threadpool. queueuserworkitem (New waitcallback (threadproc); 15 16 console. writeline ("main thread does some work, then sleeps. "); 17 // If you comment out of the sleep, the main thread exits before18 // The thread pool task runs. the thread pool uses background19 // threads, which do not keep the application running. (this20 // is a simple example of a race condition .) 21 thread. sleep (1000); 22 23 console. writeline ("main thread exits. "); 24} 25 26 // This thread procedure performs the task.27 static void threadproc (Object stateinfo) 28 {29 // No State object was passed to queueuserworkitem, so 30 // stateinfo is null.31 console. writeline ("Hello from the thread pool. "); 32} 33} 34}

After running the above Code:

2. The following is the sample code of the threadpool. queueuserworkitem method (waitcallback, object:

1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. text; 5 using system. threading; 6 7 namespace queueuserworkitem _ waitcallback _ OBJECT _ 8 {9 // queue the method for execution and specify the object containing the data used by the method. This method is executed when a thread pool thread becomes available. 10/* 11*12 * Public static bool queueuserworkitem (waitcallback callback, object state) 13*14*15 * callback type: system. threading. waitcallback16 * waitcallback, which indicates the method to be executed. 17*18*19 * State type: system. object20 * indicates the object that contains the data used by the method. 21*22*23 * return value type: system. boolean24 * true if this method is successfully queued; notsupportedexception is thrown if this work item cannot be queued. 25*26*27 * Note: If the callback method requires complex data, you can define classes containing the data. 28*29 */30 class program31 {32 static void main (string [] ARGs) 33 {34 // create an object containing the information needed for the task.35 taskinfo Ti = new taskinfo ("This report displays the number {0 }. ", 42); 36 37 // queue the task and data.38 threadpool. queueuserworkitem (New waitcallback (threadproc), Ti); 39 40 console. writeline ("main thread does some work, then sleeps. "); 41 42 // If you comment out the sleep, the main thread exits before43 // The threadpool task has a chance to run. threadpool uses 44 // background threads, which do not keep the Application 45 // running. (This is a simple example of a race condition .) 46 thread. sleep (1000); 47 48 console. writeline ("main thread exits. "); 49} 50 51 // The thread procedure performs the independent task, in this case52 // formatting and printing a very simple report.53 // 54 static void threadproc (Object stateinfo) 55 {56 taskinfo Ti = (taskinfo) stateinfo; 57 console. writeline (TI. boilerplate, Ti. value ); 58} 59} 60 61 // taskinfo holds state information for a task that will be62 // executed by a threadpool thread.63 public class taskinfo64 {65 // state information for the task. these members66 // can be implemented as read-only properties, read/write67 // properties with validation, and so on, as required.68 Public String boilerplate; 69 public int value; 70 71 // public constructor provides an easy way to supply all72 // the information needed for the task.73 public taskinfo (string text, int number) 74 {75 boilerplate = text; 76 value = number; 77} 78} 79}

After running the above Code:

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.