C#多线程 在线等 速度结贴

wenjunsu 2009-11-02 10:26:34

我想写这么一个线程

Thread k = new Thread(new ThreadStart(copyFile(newLogicDisc[0],aimPath)));

可是VS提示说“应输入方法名称”,换句话说“方法不该带参数”
但是我改成这样

Thread k = new Thread(new ThreadStart(copyFile));

这个又不是我写的方法,因为我写的方法需要传参数。。

请问我该如何在ThreadStart的方法中添加我需要的参数???
...全文
177 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
你妹的特盗不 2009-11-02
  • 打赏
  • 举报
回复
这样是可以的


//申明
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());
}

绝对可以
toyeee 2009-11-02
  • 打赏
  • 举报
回复
就是把你的copyFile的那2个参数
包装到一个类里面

lz结贴



-----------------------------------------
虽我拥有此宇宙, 无有一物为我留, 因我不可知未知, 如我不愿弃已知
龙宜坡 2009-11-02
  • 打赏
  • 举报
回复
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'
*/
wenjunsu 2009-11-02
  • 打赏
  • 举报
回复
http://www.cnblogs.com/OceanChen/archive/2009/02/02/1382426.html
happy664618843 2009-11-02
  • 打赏
  • 举报
回复
用Action委托 直接调用方法
你妹的特盗不 2009-11-02
  • 打赏
  • 举报
回复
copyFile 的参数改成obj的,然后用
Thread k = new Thread(new ThreadStart(copyFile));
k.start(你的参数);
wenjunsu 2009-11-02
  • 打赏
  • 举报
回复
你的意思是吧方法声明为delegate就ok??可惜不行啊。
toyeee 2009-11-02
  • 打赏
  • 举报
回复
比如声明结构体


Strut MyParameter
{
private String newLogicDisc;
private String aimPath;
//其他定义
}


初始化一个MyParameter

thread.Start(myParameter );
Rlien 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 jsoner 的回复:]
public delegate void copyFile(类型 newLogicDisc[0],类型 aimPath );
Thread k = new Thread(new ThreadStart(copyFile));
k.Start();
[/Quote]

这样不是可以吗?
jsoner 2009-11-02
  • 打赏
  • 举报
回复
public delegate void copyFile(类型 newLogicDisc[0],类型 aimPath );
Thread k = new Thread(new ThreadStart(copyFile));
k.Start();
gomoku 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wenjunsu 的回复:]
MyTask 又从哪儿来的???using什么??
[/Quote]

你自己写:)
叫它YourTask也可以,直接用object[2]也可以(不过可读性不好),能打包两个参数就可以。
wenjunsu 2009-11-02
  • 打赏
  • 举报
回复
MyTask 又从哪儿来的???using什么??
jsoner 2009-11-02
  • 打赏
  • 举报
回复
(copyFile(newLogicDisc[0],aimPath))这有问题.参数不在这里写.这里只写方法名
toyeee 2009-11-02
  • 打赏
  • 举报
回复
//线程代理的方法
copyFile(object obj);

//声明一个组合类或者结构体传给obj,包含你的参数newLogicDisc[0],aimPath就可以了
gomoku 2009-11-02
  • 打赏
  • 举报
回复
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 );
wenjunsu 2009-11-02
  • 打赏
  • 举报
回复
我的VS2008提示错误“Start”方法没有采用“2”个参数的重载
wenjunsu 2009-11-02
  • 打赏
  • 举报
回复
不行。。
toyeee 2009-11-02
  • 打赏
  • 举报
回复
LZ结贴了!!!!

可以这样:


//直接用方法名声明线程
Thread thread = new Thread(copyFile);
//线程运行,传入copyFile的参数
thread.Start(obj);

wenjunsu 2009-11-02
  • 打赏
  • 举报
回复

Thread k = new Thread(new ThreadStart
怎么写?
gomoku 2009-11-02
  • 打赏
  • 举报
回复
k.Start( parameter );

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧