多线程如何传递参数?

lonelyair 2006-03-26 10:55:32
多线程如何传递参数?
譬如:
private void test(string name);

发给我谢谢:kanblin@163.com
...全文
1313 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
lonelyair 2006-03-27
  • 打赏
  • 举报
回复
各位谢谢了
该结帐了~~~
这么多人热心,有分的别嫌少,没分的不要生气啊,小弟只有这点分。
愿意向各位学习,加我QQ:253844339
要毕业了,还什么都不会。
pyuan 2006-03-27
  • 打赏
  • 举报
回复
.NET Framework 2.0里有个新的ParameterizedThreadStart委托,可以用来传一个object的参数
下面是摘自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.
//
Thread newThread = new Thread(
new ParameterizedThreadStart(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.
//
Work w = new Work();
newThread = new Thread(
new ParameterizedThreadStart(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'
*/

wshcdr 2006-03-27
  • 打赏
  • 举报
回复

你可以把新开的线程封装到一个类中,如:
public class subClass
{
int a;
public subClass(int intTemp)
{
this.a = intTemp;
}
public void Thread1()
{
//code
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
subClass a = new subClass(10);//参数10
Thread t = new Thread(new ThreadStart(a.Thread1));
t.Start();
}




/////////////////////////
这个不错啊
Knight94 2006-03-27
  • 打赏
  • 举报
回复
参看
http://blog.csdn.net/knight94/archive/2006/03/21/631238.aspx
computerclass 2006-03-27
  • 打赏
  • 举报
回复
我的不能保存在本机中,mark here.
掐死温柔 2006-03-27
  • 打赏
  • 举报
回复
路过...
泡沫游走 2006-03-26
  • 打赏
  • 举报
回复
你可以把新开的线程封装到一个类中,如:
public class subClass
{
int a;
public subClass(int intTemp)
{
this.a = intTemp;
}
public void Thread1()
{
//code
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
subClass a = new subClass(10);//参数10
Thread t = new Thread(new ThreadStart(a.Thread1));
t.Start();
}

无雨就是晴天 2006-03-26
  • 打赏
  • 举报
回复

线程调用的类的方法不能有返回值和参数。你通过定义新类并设置该类的属性或字段值把参数传入。如果要调用控件等,可以在构造函数里传入。
diandian82 2006-03-26
  • 打赏
  • 举报
回复
把线程包装成类,把参数通过构造函数传进去是不错的方法.
老问题了
達魔 2006-03-26
  • 打赏
  • 举报
回复
C#中的多线程函数是不允许有参数的,
如果要实现参数传递,你可以通过类内的其它属性或域的方式把参数传递进要执行的函数体内
注意,不要忘记使用lock函数,关于lock的用法MSDN中有详细的说明我就在这里粘了,你自己一看就明白了。
lonelyair 2006-03-26
  • 打赏
  • 举报
回复
up一下
marvelstack 2006-03-26
  • 打赏
  • 举报
回复
使用类实例的参数,完整例子看,
http://blog.csdn.net/zhzuo/archive/2004/06/10/22037.aspx
bbwolfcool 2006-03-26
  • 打赏
  • 举报
回复
启动前把参数放入线程名,在线程内部,获取这个线程名
bbwolfcool 2006-03-26
  • 打赏
  • 举报
回复
如果参数简单,我原来做过一个呆方法,
把参数放在线程名称里面,如果多参数,那就用分割符分割

110,533

社区成员

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

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

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