C # Basics _ Multithreading

Source: Internet
Author: User

Single threaded: Only one thread (main thread), the disadvantage, a time can only call a function method, if you are printing time to close the program, it will not close, there is a phenomenon of suspended animation

Thread is divided into foreground thread and background thread
The default is to create a foreground thread
Foreground thread: Only a few foreground threads are closed to complete the program's shutdown
Background thread: As long as all foreground threads end, the background thread ends automatically
Resolution: The foreground thread that was created can continue to execute after the master thread is closed, and the background threads close as the foreground closes

1. Multi-threading, creating a thread

1   Static voidMain (string[] args)2         {3             //Create a thread to execute the test () method4Thread th =NewThread (Test);5             //start (); Mark the thread is ready to be executed at any time. 7Th. IsBackground =true;//set as Background thread8           //control.invokerequired9 th. Start ();Ten  One Console.readkey (); A         } -  -  the          Public Static voidTest () -         { -             -}

The set thread is the foreground thread, the background thread, and the default is the foreground thread
Set to True is a background thread, false is the foreground thread
Th. IsBackground = true; Set as Background thread

Terminates the thread,
Th. Abort ();
If the program is terminated and cannot be opened again, an exception will be thrown and the thread may have been opened or terminated.
Th. Abort ();
Th. Start ();

Cross-thread access is not allowed under. NET if a new thread is to access the main thread's resources
Workaround: Cancel cross-thread access

Sleeps 1000 milliseconds, sleeps on the current thread
Thread.Sleep (1000);

C # Basics _ Multithreading

Related Article

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.