C# 线程里面 传参怎么传啊?

ericle03 2009-04-28 11:47:15
一个很简单的例子


有个方法

public void run(string name,string id)
{
}

起一条线程, Thread thread=new Thread(run);
现在问题出来了, 我的 run 里面的参数如何传进去哦?
...全文
1196 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sysping1 2009-04-29
  • 打赏
  • 举报
回复
 
Thread thread = new Thread(new ParameterizedThreadStart(this.PRun));
thread.Start(new string[] { "刘德华", "9001" });

public void PRun(object paras) {
string[] realParas = (string[])paras;
Run(realParas[0], realParas[1]);
}

public void Run(string name, string id){
...
}
LemIST 2009-04-29
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20090421/11/EBA3E847-F655-4A3C-9FDF-E60388573674.html
LemIST 2009-04-29
  • 打赏
  • 举报
回复
近期项目不是很紧张,就抽空看了下线程方面的书,下面是自己写的一些读书笔记,也算是自己在博客园收集资料的一个开始吧。
线程操作主要用到Thread类,他是定义在System.Threading.dll下。使用时需要添加这一个引用。该类提供给我们四个重载的构造函数(以下引自msdn)。

Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托。
Thread (ThreadStart) 初始化 Thread 类的新实例。
由 .NET Compact Framework 支持。

Thread (ParameterizedThreadStart, Int32) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托,并指定线程的最大堆栈大小。
Thread (ThreadStart, Int32) 初始化 Thread 类的新实例,指定线程的最大堆栈大小。
由 .NET Compact Framework 支持。



我们如果定义不带参数的线程,可以用ThreadStart,带一个参数的用ParameterizedThreadStart。带多个参数的用另外的方法,下面逐一讲述。

一、不带参数的

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace AAAAAA
{
class AAA
{
public static void Main()
{
Thread t = new Thread(new ThreadStart(A));
t.Start();

Console.Read();
}

private static void A()
{
Console.WriteLine("Method A!");
}
}
}


结果显示Method A!

二、带一个参数的

由于ParameterizedThreadStart要求参数类型必须为object,所以定义的方法B形参类型必须为object。

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace AAAAAA
{
class AAA
{
public static void Main()
{
Thread t = new Thread(new ParameterizedThreadStart(B));
t.Start("B");

Console.Read();
}

private static void B(object obj)
{
Console.WriteLine("Method {0}!",obj.ToString ());

}
}
}


结果显示Method B!

三、带多个参数的

由于Thread默认只提供了这两种构造函数,如果需要传递多个参数,我们可以自己将参数作为类的属性。定义类的对象时候实例化这个属性,然后进行操作。

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace AAAAAA
{
class AAA
{
public static void Main()
{
My m = new My();
m.x = 2;
m.y = 3;

Thread t = new Thread(new ThreadStart(m.C));
t.Start();

Console.Read();
}
}

class My
{
public int x, y;

public void C()
{
Console.WriteLine("x={0},y={1}", this.x, this.y);
}
}
}


结果显示x=2,y=3

四、利用结构体给参数传值。

定义公用的public struct,里面可以定义自己需要的参数,然后在需要添加线程的时候,可以定义结构体的实例。

//结构体
struct RowCol
{
public int row;
public int col;
};

//定义方法
public void Output(Object rc)
{
RowCol rowCol = (RowCol)rc;
for (int i = 0; i < rowCol.row; i++)
{
for (int j = 0; j < rowCol.col; j++)
Console.Write("{0} ", _char);
Console.Write("\n");
}
}
steven_007 2009-04-29
  • 打赏
  • 举报
回复
struct IpAndPort
{
public string Ip;
public string Port;
}

public class DownLoader
{
public string Url;
public DownLoader(string url)
{
Url=url;
}


public event EventHandler StateChanger;

public void download()
{
IpAndPort aa = new IpAndPort();
aa.Ip = Url;
aa.Port = Url.Substring(Url.LastIndexOf('/'));
Thread thr = new Thread(mydown);
thr.Start((object)aa);
// thr.Join();
}

private void mydown(object aa)
{
IpAndPort ip = (IpAndPort)aa;
WebClient wc = new WebClient();
wc.DownloadFile(ip.Ip, "C:/test/" + ip.Port);
if (StateChanger != null)
StateChanger(this, EventArgs.Empty);

}

}
我姓区不姓区 2009-04-29
  • 打赏
  • 举报
回复

static void Main(string[] args)
{
string name = "sfsfsf";
string id = "12323";
Thread th = new Thread(new ThreadStart(delegate()
{
run(name, id);
}));
th.Start();
Console.Read();
th.Abort();
}

public static void run(string name, string id)
{
Console.WriteLine(name + "," + id);
}
南哥1207 2009-04-29
  • 打赏
  • 举报
回复
up,解决了,5楼领分吧~
蓝海D鱼 2009-04-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 LemIST 的回复:]
近期项目不是很紧张,就抽空看了下线程方面的书,下面是自己写的一些读书笔记,也算是自己在博客园收集资料的一个开始吧。
线程操作主要用到Thread类,他是定义在System.Threading.dll下。使用时需要添加这一个引用。该类提供给我们四个重载的构造函数(以下引自msdn)。

Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托。
Thread (ThreadStart) 初始化 …
[/Quote]up
feifeiyiwen 2009-04-29
  • 打赏
  • 举报
回复
写的不错
gxj760998 2009-04-29
  • 打赏
  • 举报
回复
msdn上面说的很清楚了吧。。。。
wenjie0728 2009-04-29
  • 打赏
  • 举报
回复
五楼写的不错
呵呵
beckfun 2009-04-29
  • 打赏
  • 举报
回复
我一直使用构造函数来传值的......惭愧.....
a7758526 2009-04-29
  • 打赏
  • 举报
回复
ParameterizedThreadStart(object)
A海阔天空 2009-04-29
  • 打赏
  • 举报
回复
学习来..
zgke 2009-04-29
  • 打赏
  • 举报
回复
ParameterizedThreadStart

或则做个线程类 传递类方法.
NetCloud0 2009-04-29
  • 打赏
  • 举报
回复
学习
maplesept 2009-04-29
  • 打赏
  • 举报
回复


Thread thread=new Thread(new ParameterizedThreadStart(run));
thread.Start("用腿")
marvelstack 2009-04-29
  • 打赏
  • 举报
回复
ThreadStart委托不支持带参数的方法,

在.net 2.0后提供了ParameterizedThreadStart委托,支持参数,定义如下,
public delegate void ParameterizedThreadStart(Object obj);

你的问题解决如下,

Thread thd = new Thread(new ParameterizedThreadStart(connect));
thd.Start(123);
///线程方法
void connect(object nId)
{
nId.ToString();
}

yangqidong 2009-04-28
  • 打赏
  • 举报
回复
最简单的办法
public void run(string name,string id)
{
}

public void sommethod()
{
string myname="xxx";
string myid="yyy";
Thread thread = new Thread(
new ThreadStart(
delegate { run(myname, myid); }
)
);
thread.Start();
}
辰爸 2009-04-28
  • 打赏
  • 举报
回复
学习,难道调用这个方法的时候不能赋值吗?

110,536

社区成员

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

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

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