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}