yesterday, a classmate asked me about multi-threaded things, really dish chicken each other peck! I think it's my own arrangement, but also review. Rookie Don't squirt!
1 usingSystem;2 usingSystem.Collections.Generic;3 usingsystem.linq;4 usingsystem.text;5 usingsystem.threading;6 usingSystem.Runtime.Remoting.Messaging;7 8 namespaceThreadTest9 {Ten public Delegate voidDelegatehandler (stringAstringb); one public class program a { - Static voidMain (string[] Args) - { the //Create thread primitives to instantiate a thread object each time a new thread is enabled -Thread Threada =NewThread (NewThreadStart (PrintA)); - Threada.start (); - + //Create a thread, no difference, C # syntax sugar notation (i've always used This) -Thread threadb =NewThread (printb); + Threadb.start (); a at //Most simplified notation - NewThread (printc). Start (); - - /* - * Task replaces ThreadPool (thread pool) after. Net 4.0 - * Programmers are too bothered to start one thread at a time, then new threads are put into the thread pool, in * Can be understood to be placed in a list (generic:list<thread>), when the function is called to remove a free thread from the list - * After execution, put it back. to */ + System.Threading.Tasks.Task.Factory.StartNew (printd); - the //with a parameter, the function parameter must be an object *Thread THREADN =NewThread (printn); $Threadn.start ("N");Panax Notoginseng - //with a parameter, the function parameter must be an object theThread Threadm =NewThread (NewParameterizedthreadstart (printn)); +Threadm.start ("M"); a the /* + * with multiple parameters - * Method 1: Pass in the parameter drop array $ * Method 2: Create a new Non-parametric method to invoke the Multi-parameter function, parameters in the Non-parameter method of the value (the most stupid method of the Person) $ * Method 3: Create a new class, write the called function and a number of parameters in this class, the main program call (same as method 2) - * Method 4: Delegate - * There are a lot of other ways to do this on the web, using delegates to achieve the */ -Delegatehandler DH =NewDelegatehandler (printq);WuyiDh"P","Q"); the - for(inti =0; I < +; i++) wu { -Console.Write ("X"); about } $ Console.readkey (); - } - #regionPrint - Static voidPrintA () a { + for(inti =0; I < +; i++) the { -Console.Write ("A"); $ } the } the the Static voidPRINTB () the { - for(inti =0; I < +; i++) in { theConsole.Write ("B"); the } about } the the Static voidPRINTC () the { + for(inti =0; I < +; i++) - { theConsole.Write ("C");Bayi } the } the - Static voidPrintd () - { the for(inti =0; I < +; i++) the { theConsole.Write ("D"); the } - } the the Static voidPRINTN (ObjectObj) the {94 for(inti =0; I < +; i++) the { the Console.Write (obj. ToString ()); the }98 } about - Static voidPRINTQ (stringAstringB)101 {102 for(inti =0; I < +; i++)103 {104Console.Write (a +b); the }106 }107 #endregion108 }109}
More code, update ing ...
C # multithreading (getting Started)