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# 中传递多个参数给多线程的更多相关文章

  1. MyBatis 中传递多个参数的 4 种方式

    方式 1 :封装成对象入参  #{对应实体类的属性} //UserMapper.java 接口 /** * 多条件查询:根据用户名称(模糊查询)和用户角色查询用户列表(参数:对象入参) * @para ...

  2. Mybatis中传递多个参数的方法总结

    一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...

  3. Mybatis接口中传递多个参数

    1.接口 public interface MemberMapper { public boolean insertMember(Members member); public Members sel ...

  4. 使用NSOperation使用,创建线程中传递多个参数

    参考:http://blog.csdn.net/dqjyong/article/details/7677557 参考:http://stackoverflow.com/questions/232761 ...

  5. struts2 action中传递两个参数到url

    <action name="outInDetail" class="formManage_outInDetailAction"> <resul ...

  6. vue v-show与v-for同时配合v-bind使用并在href中传递多个参数的使用方法

    最近在项目中,因为还没使用前端构建工具,还在使用vue+jquery方法渲染页面 碰到几个小问题,在此记录下作为vue学习之路上的一个小知识点 需求:1.数据列表存在与否状态,没有数据显示默认提示,有 ...

  7. A标签中传递的中文参数到Servlet 后台request.getParameter()接收时出现中文乱码

    package util; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequ ...

  8. MyBatis 示例-传递多个参数

    映射器的主要元素: 本章介绍 select 元素中传递多个参数的处理方式. 测试类:com.yjw.demo.MulParametersTest 使用 Map 传递参数(不建议使用) 使用 MyBat ...

  9. spring mvc 后端获得前端传递过来的参数的方法

    1.通过HttpServletRequest 获得 HttpServletRequest.getParameter(参数名),可以获得form表单中传递的参数,或ajax或url中传递过来的参数,如果 ...

随机推荐

  1. 项目常用jquery/easyui函数小结

    #项目常用jquery/easyui函数小结 ##背景 项目中经常需要使用到一些功能,封装.重构.整理后形成代码沉淀,在此进行分享 ##代码 ```javascript /** * @author g ...

  2. RSS阅读器&BT sync

    ①RSS阅读器? 答:RSS阅读器是一种软件或是说一个程序,这种软件可以自由读取RSS和Atom两种规范格式的文档,且这种读取RSS和Atom文档的软件有多个版本,由不同的人或公司开发,有着不同的名字 ...

  3. 转】Spark DataFrames入门指南:创建和操作DataFrame

    原博文出自于: http://blog.csdn.net/lw_ghy/article/details/51480358 感谢! 一.从csv文件创建DataFrame 本文将介绍如何从csv文件创建 ...

  4. hdu 1171 Big Event in HDU(多重背包+二进制优化)

    题目链接:hdu1171 思路:将多重背包转为成完全背包和01背包问题,转化为01背包是用二进制思想,即件数amount用分解成若干个件数的集合,这里面数字可以组合成任意小于等于amount的件数 比 ...

  5. OC:通讯录实战

    实战(使用OC的知识制作一个简易通讯录) //语法糖.笑笑语法 // NSString * string = [NSString stringWithFormat:@"string" ...

  6. AP(应付帐管理)

    --更新供应商地点 PROCEDURE update_vendor_site(p_init_msg_list IN VARCHAR2 DEFAULT fnd_api.g_false, x_return ...

  7. 69道java Spring面试题和答案

    http://www.jfox.info/69-dao-java-spring-mian-shi-ti-he-da-an 目录 Spring 概述 依赖注入 Spring beans Spring注解 ...

  8. 微信分享朋友圈监听(PHP)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. How do I place a group of functions or variables in a specific section?

    http://supp.iar.com/Support/?Note=27498 EWARM v5.xx (and newer) The placement of a few functions in ...

  10. 新建VM_Script

    在Hyper-V群集中,不需要设置VM的自启动,当宿主机意外关机重新启动后,上面的VM会自动转移到另一台主机:如果另一台主机处于关机状态,则宿主机重新启动后,其VM也会自启动(如果其VM在宿主机关机前 ...