C # Passing parameters to the thread using an anonymous method (you can pass any parameter without unpacking or packing)
C # Passing parameters to the thread using an anonymous method (you can pass any parameter without unpacking or packing)
Pass data to threadstart
In the above example, we want to better distinguish the output results of each thread and let one thread output uppercase letters. We input a state word to go to complete the entire task, but we cannot use threadstart to delegate because it does not accept the parameter. Fortunately ,. net Framework defines another version of the delegate called parameterizedthreadstart, which can receive a separate object type parameter:
public delegate void ParameterizedThreadStart (object obj);
The previous example looks like this:
class ThreadTest {
static void Main() {
Thread t = new Thread (Go);
t.Start (true); // == Go (true)
Go (false);
}
static void Go (object upperCase) {
bool upper = (bool) upperCase;
Console.WriteLine (upper ? "HELLO!" : "hello!");
}
Hello!
Hello!
In the entire example, the compiler automatically deduce the parameterizedthreadstart delegate, because the go method receives a separate object parameter and writes it like this:
Thread t = new Thread (new ParameterizedThreadStart (Go));
t.Start (true);
Parameterizedthreadstart is a feature that requires packing of the desired type (bool here) before use, and it can only receive one parameter.
An alternative solution is to use an anonymous method to call a common method as follows:
static void Main() {
Thread t = new Thread (delegate() { WriteText ("Hello"); });
t.Start();
}
static void WriteText (string text) { Console.WriteLine (text); }
The advantage is that the target method (writetext here) can receive any number of parameters without packing. However, you need to put an external variable into the anonymous method, as shown below:
static void Main() {
string text = "Before";
Thread t = new Thread (delegate() { WriteText (text); });
text = "After";
t.Start();
}
static void WriteText (string text) { Console.WriteLine (text); }
After
The anonymous method opens up a strange phenomenon. When an external variable is modified by a later part of the variable, it may be inadvertently interacted through the external variable. Intentional interactions (usually through fields) are considered sufficient! Once the thread starts running, it is recommended that the external variables be processed as read-only-unless someone is willing to use the appropriate lock.
Another common method is to pass the object instance method instead of the static method into the thread. The attributes of the object instance can tell the thread what to do, as shown in the following example:
class ThreadTest {
bool upper;
static void Main() {
ThreadTest instance1 = new ThreadTest();
instance1.upper = true;
Thread t = new Thread (instance1.Go);
t.Start();
ThreadTest instance2 = new ThreadTest();
Instance2.go (); // main thread -- run upper = false
}
void Go() { Console.WriteLine (upper ? "HELLO!" : "hello!"); }