C# 中传递多个参数给多线程
1.方式一:使用ParameterizedThreadStart委托
如果使用了ParameterizedThreadStart委托,线程的入口必须有一个object类型的参数,且返回类型为void.

using System;
using System.Threading; namespace ThreadWithParameters
{
class Program
{
static void Main(string[] args)
{
string hello = "hello world"; //这里也可简写成Thread thread = new Thread(ThreadMainWithParameters);
//但是为了让大家知道这里用的是ParameterizedThreadStart委托,就没有简写了
Thread thread = new Thread(new ParameterizedThreadStart(ThreadMainWithParameters));
thread.Start(hello); Console.Read();
} static void ThreadMainWithParameters(object obj)
{
string str = obj as string;
if(!string.IsNullOrEmpty(str))
Console.WriteLine("Running in a thread,received: {0}", str);
}
}
}
以上代码只能传递一个参数,如果有时我们向线程传递给多的参数,那种方式使用将有局限性(如果用类作为对象传递参数,那么需要要另外建立一个类,也稍有点麻烦)
如果用这种方法我们密切要注意的就是ThreadMainWithParameters方法里的参数必须是object类型的,我们需要进行类型转换。此方法不作推荐。
2.方式二: 创建自定义类
定义一个类,在其中定义需要的字段,将线程的主方法定义为类的一个实例方法,请看实际的例子。

using System;
using System.Threading; namespace ThreadWithParameters
{
public class MyThread
{
private string data; public MyThread(string data)
{
this.data = data;
} public void ThreadMain()
{
Console.WriteLine("Running in a thread,data: {0}", data);
}
} class Program
{
static void Main(string[] args)
{
MyThread myThread = new MyThread("hello world"); Thread thread = new Thread(myThread.ThreadMain);
thread.Start(); Console.Read();
}
}
}
这种方法也稍有繁琐,也不是我重点想要探讨的,如果需要,自己也可以使用。
3. 方式三:采用lambda表达式
对于lambda表达式不熟悉的可以查看微软MSDN上的说明文档。此处假设你熟悉。因为在大多数使用委托的时候我们一般也可以用lambda表达式的。

using System;
using System.Threading; namespace ThreadWithParameters
{
class Program
{
static void Main(string[] args)
{
string hello = "hello world"; //如果写成Thread thread = new Thread(ThreadMainWithParameters(hello));这种形式,编译时就会报错
Thread thread = new Thread(() => ThreadMainWithParameters(hello));
thread.Start(); Console.Read();
} static void ThreadMainWithParameters(string str)
{
Console.WriteLine("Running in a thread,received: {0}", str);
}
}
}
此方法三可以作为推荐方法,代码简便,方便我们使用。上面方式三说到lambda表达式,既然lambda表达式可以使用,那么我们也可以使用delegate委托。下面主要探讨的是使用这种方法。
4. 方式三:使用delegate委托
Multhd = new Thread[ThreadCount];
thread_Separate_baseCount = C_num / ThreadCount;//每个线程的终端数
listViewEx2.Items.Clear();
for (int j = 0; j < ThreadCount; j++)
{
if (j == ThreadCount - 1)//最后一个线程
{
Multhd[j] = new Thread(delegate() { Run2( j * thread_Separate_baseCount, C_num - 1); });
}
else//其它线程
{
Multhd[j] = new Thread(delegate() { Run2( j * thread_Separate_baseCount, (j + 1) * thread_Separate_baseCount - 1); });
}
}
上面代码Run(arg1,arg2.....,argn)是我们自定义的方法,在这个方法中我们可以设置任意多个参数,看也算是简单的吧。
以上代码完全可以运行,也不会出现bug什么之类的,但是实际运行情况却令我惊讶不已,用for循环创建一个线程时,关闭重开线程不会有任何问题。因为只是一个线程,可能没有什么事。但是至二个或者二个以上时,关闭再重开线程(我需要不断关闭打开终端),程序不会报错,调试器也没有什么问题,电脑有时会毫无预兆的关机,在我们做事时毫无预兆的关机这可是一个致命的问题啊,没有谁愿意这样的。以为是偶然,重复了多次结果都一样,偶然也必然存在这种必然。
为什么一个线程时没事,多个线程时运行时是不是上面的线程都指向了同一个delegate委托的地址,才导致这样会毫无预兆的关机?这些等待我们去验证,在此热烈欢迎有经验的朋友和我一块探讨。
C# 中传递多个参数给多线程的更多相关文章
- MyBatis 中传递多个参数的 4 种方式
方式 1 :封装成对象入参 #{对应实体类的属性} //UserMapper.java 接口 /** * 多条件查询:根据用户名称(模糊查询)和用户角色查询用户列表(参数:对象入参) * @para ...
- Mybatis中传递多个参数的方法总结
一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...
- Mybatis接口中传递多个参数
1.接口 public interface MemberMapper { public boolean insertMember(Members member); public Members sel ...
- 使用NSOperation使用,创建线程中传递多个参数
参考:http://blog.csdn.net/dqjyong/article/details/7677557 参考:http://stackoverflow.com/questions/232761 ...
- struts2 action中传递两个参数到url
<action name="outInDetail" class="formManage_outInDetailAction"> <resul ...
- vue v-show与v-for同时配合v-bind使用并在href中传递多个参数的使用方法
最近在项目中,因为还没使用前端构建工具,还在使用vue+jquery方法渲染页面 碰到几个小问题,在此记录下作为vue学习之路上的一个小知识点 需求:1.数据列表存在与否状态,没有数据显示默认提示,有 ...
- A标签中传递的中文参数到Servlet 后台request.getParameter()接收时出现中文乱码
package util; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequ ...
- MyBatis 示例-传递多个参数
映射器的主要元素: 本章介绍 select 元素中传递多个参数的处理方式. 测试类:com.yjw.demo.MulParametersTest 使用 Map 传递参数(不建议使用) 使用 MyBat ...
- spring mvc 后端获得前端传递过来的参数的方法
1.通过HttpServletRequest 获得 HttpServletRequest.getParameter(参数名),可以获得form表单中传递的参数,或ajax或url中传递过来的参数,如果 ...
随机推荐
- 30+学习Web设计和开发的优质新鲜资源
今天我们整理了一些最新的Web设计和开发的资源,这些资源都来自国外的流行站点,不过大家应该不会陌生,放在这里供大家收藏,在需要的时候方便翻阅和学习! 原文地址:http://www.goodfav.c ...
- Windows Server 2003单网卡搭建VPN
Windows Server 2003单网卡搭建VPN 1.打开[控制面板] --> [管理工具] --> [路由和远程访问] 2.鼠标右击你要管理的电脑 在弹出式菜单中选中[配置并启 ...
- iOS 检测有没有安装其它应用 和ios9下要注意的地方
UIApplication *app = [UIApplication sharedApplication]; NSURL *url = [NSURL URLWithString:@"Tri ...
- initWithSpriteFrameName和createWithSpriteFrameName
/** * Initializes a sprite with a sprite frame name. <br/> * A cc.SpriteFrame will be fetched ...
- openfire 最大连接数调优
https://community.igniterealtime.org/thread/48064#224126 https://community.igniterealtime.org/thread ...
- 解决Windows时间同步失败问题!系统时间同步设置!
使用NTP协议可以让你的计算机自动与服务器上的时间同步.从而保持最准确的时间. 中国国家授时中心的IP地址是:210.72.145.44 (至少我一直没ping通) 在Windows XP/2000/ ...
- 时隔3年,再次折腾BlackBerry 8830!
2010年手头换得8830,之后就是好几番刷机.解SPC.倒腾各种软件..算软件注册码..那个时候记得最难弄的注册码就是crunchSMS.需要运行虚拟机来从内存地址读取注册码..不过黑莓真的很经得起 ...
- [原创]mac终端前面的计算机名怎么改??
1.修改-之前的名称 mac环境,系统 OS X Yisemite,打开终端, 执行下面命令“Tmp”是你想要改的电脑名称 sudo scutil --set HostName Tmp 执行前,执行后 ...
- SharePoint 2013的100个新功能之社交
一:社会能力 SharePoint 2013引入了一个新东西叫做社会能力,使公司组织中的用户社会化协作.我的网站难以置信地做了改进以集成社会能力.除了我的网站,新的社区网站(新闻提要),关注用户和关注 ...
- 51Nod 1201 整数划分 (经典dp)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1201 题意不多说了. dp[i][j]表示i这个数划分成j个数 ...