Asynchronously monitors whether a thread (task) is fully executed by replacing ThreadPool with a task

Source: Internet
Author: User

Do you want to determine if thread pool threads are complete?
Using microsoft.visualstudio.testtools.unittesting;using system.collections.generic;using System.Threading;using System.threading.tasks;namespace ex.rulestester.threads{///<summary>///Async wait for all threads to end notification test///</summary&    Gt        [TestClass] public class Taskallcompletetester {[TestMethod] public void Testtaskpoolstatus ()            {//Run asynchronous multi-task Tasktest.runtask ();            var list = Tasktest.successlist;            BOOL Isallcomplete = false;                Waits for an asynchronous notification while (!isallcomplete) {isallcomplete = Tasktest.isallcomplete;            list = Tasktest.successlist;        } assert.istrue (True);        }///<summary>//package for Task multitasking//</summary> private class Tasktest            {private static object M_lock = new Object ();            public static list<string> successlist = new list<string> (); Private static string msg = "";            public static bool Isfinish = FALSE;            public static list<task> TaskList = new list<task> ();            static bool Isallcomplete = FALSE; <summary>///Mark All tasks complete or not,//</summary> public static bool Isallcompl            ete {get {return isallcomplete;}                } private Tasktest () {} public static void RunTask () { Run 5 tasks for (int i = 1; I <= 5; i++) {var task = Task.Factory.St                    Artnew (Dotask, i);                Tasklist.add (Task); }//Asynchronously waits for all tasks to complete Task.Factory.StartNew (x = {task.wait                    All (Tasklist.toarray ());                Mark All Tasks Run complete Isallcomplete = true;            }, NULL); } Private STATIC void Dotask (object par) {thread.sleep (int). Parse (par.                ToString ()) * 1000); Lock (M_lock) {Successlist.add ("has completed T" + par.                ToString ()); }            }        }    }}



Grass Green Studio

Asynchronously monitors whether a thread (task) is fully executed by replacing ThreadPool with a task

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.