C#多线程间传入值和返回值问题(二)
不说那么多废话,直接上代码,不懂看注释!站在代码面前,一切问题将不再是问题!
一、不带参数的线程class main
{
public static void main(string[] args)
{
Thread th1 =new Thread(DoWork);
th1.Start();Thread th2
{
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;
}
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();
}
}
ThreadWithParams twp =new ThreadWithState( "i am param1", 2);
Thread t =new Thread(new ThreadStart(twp.ThreadProc));
t.Start();
}
}
语法高亮插件有点问题,所以代码不漂亮,理解!
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · C#多线程间传入值和返回值问题
- · The instance of entity type ‘Customer’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked.
- · .NET8实时更新nginx ip地址归属地
- · 解决.NET Blazor子组件不刷新问题
- · .NET8如何在普通类库中引用 Microsoft.AspNetCore
- · .NET8 Mysql SSL error
- · ASP.NET Core MVC的Razor视图渲染中文乱码的问题
- · .NETCORE 依赖注入服务生命周期
- · asp.net zero改mysql
- · .NET5面试汇总
- · .Net连接Mysql数据库的Convert Zero Datetime日期问题
- · vue使用element-ui中的Message 、MessageBox 、Notification
1呢?