Some basic concepts:
All the resources required to run a program are a process.
A process can have multiple threads and a minimum of one thread.
A thread is the smallest unit of CPU scheduling.
The process can be understood as a company, a thread understood as an employee in the company.
Application domain: You can start multiple applications in one process (another exe in one EXE program and only one process ~ in Task Manager).
When do you use multithreading?
1, a task takes a long time, but we do not want to wait, want to do in this task, while doing something else
2, the CPU is more busy, other operations are more busy when you can use multi-threaded.
3, in order to prevent the interface card dead, you can use multithreading.
The following code:
1. Start a thread and execute a method with no parameters
1. Create a thread object and pass the DoWhile method in, indicating that the thread will execute this method when it is started T1 = new Thread (new ThreadStart (DoWhile));
T1. IsBackground = true; Sets the current thread as a background thread, and the background thread exits automatically after all foreground threads have finished executing. The process created by default is the foreground thread
{
while (true)
{
Console.WriteLine (DateTime.Now.ToString ("Yyyy-mm-dd hh:mm:ss"));
Thread.Sleep (500);
}
}
2. Start a thread and execute a method that requires a parameter
thread T1 = new Thread (new Parameterizedthreadstart (M2)); T1. Start (+); static void M2 (object N) { int max = (int) n; int sum = 0; for (int i = 1; i < Max; i++) { sum + = i; } Console.WriteLine (sum); }
3, start a thread, execute the method with parameters, a few parameters are OK!
Encapsulates the method to be executed into a class// encapsulates the parameters required by the execution method into the properties of the class //Changes the method to a non-parameter method, and the parameters in the original method are replaced with the properties of the class. //Assign a value to a property of this class, which is equivalent to a method parameter MyClass mc = new MyClass (); Mc. Number = 101; thread T1 = new Thread (new ThreadStart (MC). M3)); T1. Start (); public class MyClass {public int number { get; Set; } public void M3 () { int sum = 0; for (int i = 1; i < this. number; i++) { sum + = i; } Console.WriteLine (sum); } }
4. Start a thread and execute the method with the return value
Create a background Performer object BackgroundWorker backworker = new BackgroundWorker (); Sets the method that the object executes in the background backworker.dowork + = new Doworkeventhandler (backworker_dowork); Sets the event that fires when the background method finishes executing backworker.runworkercompleted + = new Runworkercompletedeventhandler (backworker_ runworkercompleted); Start execution of Backworker.runworkerasync (new int[] {1, +}); Method for background execution static void Backworker_dowork (object sender, DoWorkEventArgs e) { if (e.argument! = null) { int[] arr = e.argument as int[]; Assigns the return value to E. Result E.result = Sum (Arr[0], arr[1]); } } Event triggered after the background method finishes executing static void Backworker_runworkercompleted (object sender, Runworkercompletedeventargs e) { Console.WriteLine ("Last output is: {0}", E.result); }
Multi-threaded Learning