Brief description

Generally one program and one process,The code is in the process,The process itself does not execute code, It is the threads that execute the code.

Generally there is one thread in one process.(A shop is a boss.)

Process is to open up a space in memory.Code, picture. . Wait in this space.The code thread goes to execute.

There is only one thread by default.

systerm.threading //The thread operation class is under this namespace.

2. Foreground and background threads.

Start a thread,Just create a thread object.

Threads are foreground threads by default.

After executing all foreground threads,The program will exit.

default The default thread in the process is called the main thread or the default thread.Or ui threads.

Background thread,As soon as all foreground threads have ended,All background threads end directly.

thread th=new thread (sum);
th.name="Thread 1";Name the thread.
When debugging, you can see the specific name in the output.
th.start ();
th.abort ();Forcibly terminates the thread.
thread.sleep (1000);pauses the thread,The unit is millisecond
thread cuth=thread.currentthread gets a reference to the current thread.
thread type.
textbox.checkforillegalcrossthreadcalls=false;//Turn off cross-thread access checks for space.
 Otherwise, the following error will occur

3. Thread reentry

carried out Multiple threads in the same process can execute "concurrently".

Multiple threads access the same resource, May cause out-of-sync conditions,This is called thread reentrancy.

I should respond to this situation,Can be locked.

private void countnum ()
 lock (this)
 for (int i=0;i<10000;i ++)
 int num=int.parse (textbox1.text.trim ());
 num ++;
 textbox1.text=num.tostring ();

4. Thread Object Nature (Delegation)

Need to pass a delegate object.

threadstart ts=new threadstart (countnum);thread th=new thread (ts);is equivalent to thread th=new thread (countnum);essentially a delegate object.
 (Countnum is a parameterless method)
4.2 Parametric method

If you need to pass a reference method.

Is the delegate object of the parameterizedthreadstart created.

Passing parameters is passed in the start method;The start method has two overloads.

  • Previous Simple understanding of the pynotify package for monitoring file systems under Python
  • Next C # inline-asm solve embedded x86 assembly