Thread k = new Thread(new ThreadStart(copyFile(newLogicDisc[0],aimPath))); 可是VS提示说“应输入方法名称”,换句话说“方法不该带参数” 但是我改成这样 Thread k = new Thread(new ThreadStart(copyFile)); 这个又不是我写的方法,因为我写的方法需要传参数。。 请问我该如何在ThreadStart的方法中添加我需要的参数???
LZ结贴了!!!!可以这样: //直接用方法名声明线程 Thread thread = new Thread(copyFile); //线程运行,传入copyFile的参数 thread.Start(obj);
我的VS2008提示错误“Start”方法没有采用“2”个参数的重载
ParameterizedThreadStart只能带一个参数,但你可以自己定义该参数:void CopyFile(object state) { MyTask task = state as MyTask; copyFile( task.Source, task.Destination ); }Thread k = new Thread( new ParameterizedThreadStart(CopyFile) ); MyTask task = new MyTask(newLogicDisc[0],aimPath); k.Start( task );
Thread.Start 方法 (Object)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 k = new Thread(new ThreadStart
怎么写?
//直接用方法名声明线程
Thread thread = new Thread(copyFile);
//线程运行,传入copyFile的参数
thread.Start(obj);
{
MyTask task = state as MyTask;
copyFile( task.Source, task.Destination );
}Thread k = new Thread( new ParameterizedThreadStart(CopyFile) );
MyTask task = new MyTask(newLogicDisc[0],aimPath);
k.Start( task );
copyFile(object obj);//声明一个组合类或者结构体传给obj,包含你的参数newLogicDisc[0],aimPath就可以了
你自己写:)
叫它YourTask也可以,直接用object[2]也可以(不过可读性不好),能打包两个参数就可以。
Thread k = new Thread(new ThreadStart(copyFile));
k.Start();
Strut MyParameter
{
private String newLogicDisc;
private String aimPath;
//其他定义
}
初始化一个MyParameter thread.Start(myParameter );
Thread k = new Thread(new ThreadStart(copyFile));
k.start(你的参数);
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'
*/
包装到一个类里面lz结贴-----------------------------------------
虽我拥有此宇宙, 无有一物为我留, 因我不可知未知, 如我不愿弃已知
//申明
private System.Threading.Thread thread;
private void button1_Click(object sender, EventArgs e)
{
this.thread = new System.Threading.Thread(this.copyFile);
this.thread.start("AAA");
}//下面是你需要在线程里面运行的函数。不需要用到委托
private void copyFile1(object ss)
{
MessageBox.Show(ss.ToString());
}绝对可以