C # (7)---thread

Source: Internet
Author: User

C # supports executing code in parallel through multithreading, where a thread has its own execution path and can run concurrently with other threads. A C # program starts with a single thread, which is created automatically by the CLR and the operating system, and has multithreading to create additional threads.

Unless specified, all examples assume that the following namespaces are referenced:
Using System.Threading;

This should be added to the reference, otherwise the error 、。。。

Try a wireless output x and Y program with the result:

The output does not stop until the window is closed

In the startup of the thread:

Instantiating the thread needs to provide a delegate that is used when instantiating the delegate, which is the method to run when the thread is started in the future.

There are two ways to start a thread in. NET, one is to start without parameters, and the other is to start with a parameter.
1. Starting mode without parameters:
If no additional information is required to start the parameter, you can use ThreadStart to instantiate the thread:

The output is:

2. Starting method with parameters
If you want to take some arguments when instantiating a thread, you cannot instantiate the thread with the ThreadStart delegate as the argument to the constructor, but Parameterizedthreadstart the delegate.

It is also the method to be executed when the thread starts, but it can be instantiated with a method with an object parameter as a constructor parameter, and the method used to instantiate the ThreadStart is no parameter ...

Here, when implemented, defines the pause interval of the program, which enables the control of the interval between threads during operation:

The output is:

C # (7)---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.