Thread Pool ThreadPool implements asynchronous multithreading and threadpool Multithreading

Source: Internet
Author: User

Thread Pool ThreadPool implements asynchronous multithreading and threadpool Multithreading

Main Methods of ThreadPool thread pool:

1. public static Boolean QueueUserWorkItem (WaitCallback wc, Object state );

WaitCallback callback function: puts some callback functions into the thread pool to form a queue, and the thread pool will automatically create or reuse threads to execute and process these callback functions. State: this parameter is also very important. When a callback function with parameters is executed, this parameter will be passed in as a reference and used in the callback method.

Eg: used for File Processing: // delete an ftp directory file

DataTable dt = new ArchivesHelper (). getFileListByArchivesId (ID); // ftp file foreach (DataRow dr in dt. rows) {StateInfo sta = new StateInfo (); sta. ftpPath = dr ["filePath"]. toString (); sta. ftpThumbnailPath = dr ["Thumbnail"]. toString (); // The hreadProc method is added to the thread pool for execution. The sta object is passed into the callback function for ThreadProc to use ThreadPool. queueUserWorkItem (new WaitCallback (ThreadProc), sta );} /// <summary> /// create a delegate to reference the code executed in the auxiliary thread (an object parameter is accepted and no return value is returned) /// </summary> /// <param name = "stateInfo"> </param> private void ThreadProc (Object stateInfo) {var sta = stateInfo as StateInfo; FtpHelper. deleteFile (sta. ftpPath); // Delete the ftp file FtpHelper. deleteFile (sta. ftpThumbnailPath); // delete an ftp thumbnail}

 

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.