First, talk about process and thread.
Process: When a program is opened and running, it is a process at this moment. It is the basic unit of the operating system for resource scheduling and allocation, a process can have several threads, the thread can also help the process to do multiple things
Threads: the smallest unit of a thread's execution flow, which does not own resources, but he can share all the resources in the process with other threads in the process, and one thread can create or terminate other threads
Multithreading: In the unit time of a single CPU system, the CPU can only allow a single thread, the order depends on the thread's level {from low to High: Lowest, BelowNormal, Normal (default), Aboveboral, highest}, Multiple threads can be considered to run concurrently because they are switched frequently and the time is very short.
Ii. creating threads with the thread class
1, several often use the attribute:
The Isalive:bool type, which determines whether the thread is executed, does not execute the value after the thread is created, is true when the start () method is called, and the remaining ThreadState state is true when the thread ends with a value of false
IsBackground: Whether it is a foreground thread, creating a thread with the thread class (thread Thread=new thread ()) defaults to the foreground thread, which is Isbackground=false, Threads established by the CLR thread pool are always default to background threads . Foreground threads do not close with the main program shutdown when the main program is closed
Managedthreadid: Gets the thread unique identifier
Name: Thread Name
Priority: Thread-limited level
ThreadState: Thread state {unstarted, Running, WaitSleepJoin, and so on}
2, familiar with two delegates: public delagate void ThreadStart (), public delegate void Parameterizedthreadstart (object a)
It should be clear to see how the two delegates are used.
3, some methods in the thread:
Sleep: Suspends a running thread for a period of time
Abort: Terminates this thread
Join: Blocks the calling thread, only after the thread has completed and then continues the operation or thread
Interrupt: Interrupts a thread in Wautsleepjoin state
Start: Execute this thread
Suppend means hang, resume means resume suspended thread (no longer available, no explanation here)
Third, thread pool
ThreadPool thread pool contains QueueUserWorkItem This method is used to make asynchronous calls, which can be taken with an object
parameters, or without, default to null without parameters
Threadpool.setmaxthreads () sets the maximum number of threads in the thread pool, has two parameters, first sets the worker thread, and the first sets the IO thread
ThreadPool.QueueUserWorkItem (New WaitCallback (method name), parameters)
The above step has created a thread in the threads pool, and WaitCallback is also a delegate: public delegate void WaitCallback (object state);
It is simple to start a thread by QueueUserWorkItem, but it does not meet our needs in actual development, it can only take one parameter at most, and there is no return value.
Iv. using a delegate class to start a thread
1, the delegation class several important methods:
Invoke (); When invoking the Invoke () method, all methods corresponding to this delegate will be executed
BeginInvoke (), EndInvoke () supports asynchronous invocation of delegates, threads that are started by BeginInvoke belong to threads in the thread pool '
2, BeginInvoke parameter description:
The penultimate parameter is used to invoke external data, which is equivalent to passing parameters in the net callback function
The second-to-last argument is a callback function
Parameters in the method that match the delegate are the preceding parameters
Public delegate string GetString (String a)//declares a delegate
static void Main (string[] arg)
{
GetString getstring=new GetString (GetName);//Create a delegate and bind a method for him
An asynchronous invocation of the delegate requires the use of the IAsyncResult interface
"//This way the asynchronous call needs to wait until the async call method finishes executing before calling the main thread, which does not match the actual development needs
IAsyncResult result=getstring. BeginInvoke ("Zhang San", Null,null);
String name=getstring. EndInvoke (Result);//Gets the value returned by the method GetName
Console.WriteLine (name);
Console.WriteLine ("Main thread");
}
】
"//through member IsCompleted in IAsyncResult to get the completion of an asynchronous operation, the bool type will continue to execute the main thread when the asynchronous call operation is not completed
IAsyncResult result=getstring. BeginInvoke ("Zhang San", Null,null);
while (! result.iscompleted)
{
Console.WriteLine ("Main thread");
}
String name=getstring. EndInvoke (Result);//Gets the value returned by the method GetName
Console.WriteLine (name);
】
"//Use the WaitOne () method in Wailhandle to complete the asynchronous call
IAsyncResult result=getstring. BeginInvoke ("Zhang San", Null,null);
while (! Result.AsyncWaitHandle.WaitOne (200))//parameter represents the time, in a polling way to determine whether the asynchronous call is completed
{
Console.WriteLine ("Main thread");
}
String name=getstring. EndInvoke (Result);//Gets the value returned by the method GetName
Console.WriteLine (name);
】
"//If you need to monitor the completion of multiple asynchronous calls, we need to declare a box of WaitHandle arrays and pass the objects that need to be monitored.
IAsyncResult result=getstring. BeginInvoke ("Zhang San", Null,null);
Waithandle[] waitlist=new waithandle[]{result.asyncwaithandle,.....};
There are two main methods for monitoring 1, WaitAll (array, time), waiting for all asynchronous calls to complete after returning a bool value, 2, WaitAny
Wait Waitlist One completes an asynchronous call and returns a value of type int, which is the index of the array
while (! WaitHandle.WaitAll (waitlist,200))
{
Console.WriteLine ("Main thread");
}
】
Pubslic string GetName (string name)
{
Return "Hi" +name;
}
C # Multithreaded Learning