C # Multithreading
1. Applicable to:
Communication over the network
Perform time-consuming actions
Differentiate tasks with different priorities
Enables the user interface to respond quickly to user interactions when performing background tasks
2. common attributes and methods of Thread class
Property:
IsAlive: Show current thread execution status
IsBackground: Whether it is a background process
Name: Gets or sets the thread name
ThreadState: Gets the current thread state
Method:
Thread: constructor, delegate passed to thread when thread is started
Abort: When called, ThreadAbortExceptionis raisedand the thread begins to terminate
Join: blocks the calling thread until a thread terminates
ResetAbort: Cancels abort of the current thread request
Sleep: Sets the number of milliseconds that the thread blocks specified
Start: Begin execution
3, the status of the thread:
4. Start Thread:
1) Create a thread instance:
As shown, invoking the constructor method of the thread class instantiates a thread instance, but the actual thread is not created, at which point the thread is in the unstarted state.
Create thread instances Common: thread t = new Thread (threading method);
and delegate the thread instance to the method of executing the thread: Void Thread method () | | void Threading Method (object)
2) Create thread
T.start ();
T.start (Object obj);
5. Suspend Thread:
thread.sleep (1000);
6. Merging Threads:
You can call the Join method in T1 code if one thread T1 during execution and needs to wait for another thread to finish T2 to continue execution.
For example: T2. Join ();
7. Terminating the thread:
forcibly terminating a thread:. Abort ()
Natural Termination : When the delegate method ends, the thread ends with it.
Cond...
Lesson 7:c# Multithreading