分类

链接

2011 年 6 月
 12345
6789101112
13141516171819
20212223242526
27282930  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > .NET > 正文
共享办公室出租
C#多线程间传入值和返回值问题
.NET 暂无评论 阅读(2,462)

不喜欢说那么多废话,直接上代码,不懂看注释!站在代码面前,一切问题将不再是问题!

一、不带参数的线程class main
{
public static
void main(string[] args)
{
Thread th1
=new Thread(DoWork);
th1.Start();
Thread th2

=new Thread(new ParameterizedThreadStart(DoWorkWithParam));
object data =null;
th2.Start(data);
}

privatevoid DoWork()
{
//do something
}

privatevoid DoWorkWithParam(object param)
{
string data = param.ToString();
//do something
}}

二、带一个参数

class main
{
public static
void main(string[] args)
{
Thread th2
=new Thread(new ParameterizedThreadStart(DoWorkWithParam));
object data =null;
th2.Start(data);
}

privatevoid DoWorkWithParam(object param)
{
string data = param.ToString();
//do something
}}



三、带两个及两个以上的参数

publicclass ThreadWithParams {
privatestring param1;
privateintparam2;public ThreadWithParams(string param1, int param2 )
{
this. param1 = param1 ;
this. param2 = param2;
}

publicvoid ThreadProc()
{
Console.WriteLine(param1, param2);
}
}

publicclass Main{
publicstaticvoid Main()

          //初始化两个参数
ThreadWithParams twp
=new ThreadWithState( "i am param1", 2);
Thread t
=new Thread(new ThreadStart(twp.ThreadProc));
t.Start();

}
}

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:C#多线程间传入值和返回值问题 | Bruce's Blog

发表评论

留言无头像?