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 operating system and has multithreading to create additional threads. Here's a simple example with its output:
Results:
The main thread creates a new threads "T", which runs a repeating method of printing the letter "Y" while the main thread repeats but the letter "X". The CLR allocates each thread to its own memory stack to ensure the separation of local variables runs. In the next method, we define a local variable, and then call this method at the same time on the main thread and the newly created one.
How the thread works
A thread is managed by a thread coordinator--a function that the CLR delegates to the operating system. The thread coordinator ensures that all active threads are assigned the appropriate execution time, and that the waiting or blocking threads-such as in an exclusive lock or user input-do not consume CPU time.
On a single-core processor computer, the thread coordinator completes a time slice and quickly switches execution between active threads.
In multicore computers, multithreading is implemented as a mix of time slices and real concurrency--different threads running on different CPUs. This can almost certainly still occur some time slices, as the operating system needs to serve its own threads, as well as some other applications.
Thread vs. process
All threads that belong to a single application are logically contained in a process, which refers to the operating system unit that an application is running.
Threads are similar in some way to processes: for example, a process typically runs in a time-slice manner with other processes running on a computer in much the same way as a C # program thread. The key difference between the two is that the process is completely isolated from each other. Threads Share (heap heap) memory with other threads running in the same program, which is why threads are so useful: one thread can read data in the background, while another thread can present the data that has been read in the foreground.
When to use multithreading
Multithreaded programs are typically used to perform time-consuming tasks in the background. The main thread keeps running, and the worker thread does its background work. For Windows Forms programs, if the main thread attempts to perform time-consuming operations, the keyboard and mouse operations become dull and the program loses its response. For this reason, you should add a worker thread when running a time-consuming task in a worker thread, even if there is a good hint in "processing ..." on the main thread to prevent the work from continuing.
Other examples:
The thread t executes the go function, and the main thread also calls go, and the result is:
Anonymous functions: Multiple parameters can be passed, and no type conversions are required
Another way to pass a parameter is to pass an instance past, rather than pass a static function:
thread naming: threads have a name property that is useful when debugging.
exception handling for threads: Once a thread is started, any code blocks that create threads within the Try/catch range are not related to Try/catch.
Results No output:
Introduction to C #-----threading (i)