C # timed to detect if a child thread has completed
Class Program {static void Main (string[] args) {//////main thread starts a route, executes dosomething such a method. Thread thread = new Thread (new ThreadStart (ThreadRun)); Thread. IsBackground = true;//This can end the thread with the main program. Start (); Console.readkey (); } delegate void Delegate_do (); static void ThreadRun () {try {delegate_do Delegate_do = new Delegate_do (Find ALLPRODUCT); IAsyncResult result = Delegate_do. BeginInvoke (null, NULL); while (!result. iscompleted) {Console.WriteLine ("sub-thread not completed"); Thread.Sleep (1000);//Every 1 seconds to determine whether to complete} while (true) {if (res Ult. iscompleted) {Console.WriteLine ("-------Child thread completed-------"); Break } } }catch (Exception ex) {Console.WriteLine (ex. Message); }} static void Findallproduct () {list<int> array = new list<int> (); for (int i = 0; i < 100000000; i++) {array. ADD (i); } int m = 0; foreach (var i in array) {m++; } Console.WriteLine (M); } }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C # timed to detect if a child thread has completed