new ThreadStart(方法)中的方法如果有参数,该怎么写呢
Thread InstanceCaller = new Thread(new ThreadStart(serverObject.InstanceMethod));
private string InstanceMethod(string str,int i);
假设是这个语句,应该怎么写呢? --------------------编程问答--------------------
http://blog.csdn.net/nash603/article/details/6085206 --------------------编程问答-------------------- Thread InstanceCaller = new Thread(
new ThreadStart(delegate{serverObject.InstanceMethod(str,i);})) --------------------编程问答-------------------- ParameterizedThreadStart Delegate
参考msdn:
http://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart.aspx --------------------编程问答-------------------- private string InstanceMethod(string str,int i);
===》 private void InstanceMethod(); --------------------编程问答-------------------- msdn上的代码:
--------------------编程问答--------------------
using System;
using System.Threading;
public class Work
{
public static void Main()
{
// To start a thread using a shared thread procedure, use
// the class name and method name when you create the
// ParameterizedThreadStart delegate. C# infers the
// appropriate delegate creation syntax:
// new ParameterizedThreadStart(Work.DoWork)
//
Thread newThread = new Thread(Work.DoWork);
// Use the overload of the Start method that has a
// parameter of type Object. You can create an object that
// contains several pieces of data, or you can pass any
// reference type or value type. The following code passes
// the integer value 42.
//
newThread.Start(42);
// To start a thread using an instance method for the thread
// procedure, use the instance variable and method name when
// you create the ParameterizedThreadStart delegate. C# infers
// the appropriate delegate creation syntax:
// new ParameterizedThreadStart(w.DoMoreWork)
//
Work w = new Work();
newThread = new Thread(w.DoMoreWork);
// Pass an object containing data for the thread.
//
newThread.Start("The answer.");
}
public static void DoWork(object data)
{
Console.WriteLine("Static thread procedure. Data='{0}'",
data);
}
public void DoMoreWork(object data)
{
Console.WriteLine("Instance thread procedure. Data='{0}'",
data);
}
}
/* This code example produces the following output (the order
of the lines might vary):
Static thread procedure. Data='42'
Instance thread procedure. Data='The answer'
*/
此乃正确做法 --------------------编程问答-------------------- Thread只提供两种初始化方式
一种不带参数的ThreadStart委托
Thread th = new Thread(new ThreadStart(th_proc));
void th_proc()
{
//...
}
th.Start(); //不给参数 开启线程
一种带一个参数的ParameterizedThreadStart委托
Thread th = new Thread(new ParameterizedThreadStart(th_proc));
void th_proc(object arg)
{
int i = (int)arg;
//...
}
th.Start(100); //给定一个参数开启线程
如果多个参数,可以使用object[](或其他)
Thread th = new Thread(new ParameterizedThreadStart(th_proc));
void th_proc(object arg)
{
object[] objs = arg as object[];
//objs[0]、objs[1]、objs[2]
//...
}
th.Start(new object[]{10,100,1000}); //给定多个参数开启线程
如果你先定义线程方法 private string InstanceMethod(string str,int i);
你明显没搞清楚顺序
只有进行一次包装:
Thread th = new Thread(new ParameterizedThreadStart(th_proc));
void th_proc(object arg)
{
object[] objs = arg as object[];
string arg1 = objs[0].ToString();
int arg2 = (int)objs[1];
InstanceMethod(arg1,arg2);
//...
}
th.Start(new object[]{"test",100}); //给定两个参数开启线程
补充:.NET技术 , C#