C # Check whether the Sub-thread has been completed regularly
C # Check whether the Sub-thread has been completed regularly
Class Program {static void Main (string [] args) {// start a support thread in the Main thread and execute a method such as doSomething. Thread thread = new Thread (new ThreadStart (ThreadRun); thread. isBackground = true; // end thread with the main program. start (); Console. readKey ();} delegate void Delegate_do (); static void ThreadRun () {try {Delegate_do = new Delegate_do (FindAllProduct); IAsyncResult result = empty (null, null ); while (! Result. isCompleted) {Console. writeLine ("subthread unfinished"); Thread. sleep (1000); // judge whether it is completed every other second} while (true) {if (result. isCompleted) {Console. writeLine ("------- subthread completed -------"); break ;}} catch (Exception ex) {Console. writeLine (ex. message) ;}} static void FindAllProduct () {List
Array = new List
(); For (int I = 0; I <100000000; I ++) {array. add (I) ;}int m = 0; foreach (var I in array) {m ++;} Console. writeLine (m );}}