Thread Operation termination Thread

Source: Internet
Author: User

When a thread terminates, it is implemented by the thread class's Abort method, such as if an out-of-the-box execution time is too long, the user may want to terminate the thread, which is the way to use this method

Note: When a thread calls the Abort method, a Threadabortexcepion exception is thrown, and if no exception is caught, it terminates

Instance

If the thread that outputs the ">" character on the screen is executed through while, the number of cycles is calculated when the thread has not finished executing, and the thread is terminated when it reaches 5 times.

 classProgram { Public Static voidmeth () { for(inti =1; I <=4000; i++)            {                if(i% +==0) {Console.WriteLine (">"); }                Else{Console.Write (">"); }            }        }        Static voidMain (string[] args) {            //ThreadStart ts = new ThreadStart (meth);Thread th =NewThread (meth); Th.            Start (); Console.WriteLine ("thread starts to start"); inti =0;  while(Th. IsAlive)//Determine thread status{i++; Thread.Sleep (5);//thread hangs 5 seconds after execution, or "Start and stop" is displayed                if(i==5) {th.   Abort (); //terminating a threadConsole.WriteLine ("Thread Termination");        }} console.read (); }

Thread Operation termination Thread

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.