Problem:
For multithreaded programming, it is often necessary to pass more than one parameter to a thread, while a thread in C # receives only 1 parameters of type object (as follows):
Thread t = new Thread (new Parameterizedthreadstart (Newthread));
T.start (parameter);
void Newthread (object)
{
...}, ...
}
And now you need to pass multiple parameters to the thread, such as method methods that want to run with a separate thread
void method (int begin,int end) {..........
}
Workaround 1: Create a new class for the Run method
class MyClass
{
private int begin;
public int begin
{
set{this.begin=value}
}
private int end;
public int end
{
set{this.end=value;}
}
Public run ()
{method
(begin,end);
}
Private method (int begin,int end)
{
...
}... ...}
Then create a new instance of the class, after assigning the value can run, the code is as follows;
MyClass C = new MyClass ();
c.begin=100;
c.end=10000;
Thread t = new Thread (new ThreadStart (C.run))
T.start ();
2, Workaround 2: The array or collection instance as arguments passed in
is currently doing a multithreaded software, used in this section, feel every time to create a new class, more trouble, check the online mainstream is the solution to the method 1, and then I figured out another way, even though the new thread can only pass 1 parameters to go in, but we can pass a set or the parameters of the array class to go in, This solves the problem of passing multiple parameters to a new thread at once.
Similarly, for the method above, you need to pass 2 integers of type int, first add an overload of method methods
void method (Object o)
{
//Here the incoming parameters are processed
int[] p = (int[]) o;
Invokes the original methods method
(P[0],p[1]);
The parameters to be passed are stored in arrays or collections
int Para[]=new int[2]{100,10000};
The last new thread is called
Thread t = new Thread (new Parameterizedthreadstart (method))
T.start (para);
This allows the 2 parameters to be passed into the thread.
The parameter types passed can also be used in list<> or other collections, and the types of these arguments passed in are the same, and for different types, they can be considered in the form of list<object> or object[]. The parameters are processed in the overloaded method.
The above is a small set to the introduction of C # to the thread to pass multiple parameters of the solution (two kinds), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!