C#中创建线程,创建带参数的线程
线程操作主要用到Thread类,他是定义在System.Threading.dll下。使用时需要添加这一个引用。该类提供给我们四个重载的构造函
构造函数定义:
无参数委托
[SecuritySafeCritical]
public Thread(ThreadStart start);
[SecuritySafeCritical]
public Thread(ThreadStart start, int maxStackSize);
有一个参数object委托
[SecuritySafeCritical]
public Thread(ParameterizedThreadStart start);
[SecuritySafeCritical]
public Thread(ParameterizedThreadStart start, int maxStackSize);
// maxStackSize:
// 线程要使用的最大堆栈大小(以字节为单位);如果为 0 则使用可执行文件的文件头中指定的默认最大堆栈大小。重要地,对于部分受信任的代码,如果 maxStackSize
// 大于默认堆栈大小,则将其忽略。不引发异常。
一、创建没有参数传入线程
//创建没有参数的线程
Thread thread = new Thread(new ThreadStart(ThreadMethod));
//或者
//Thread thread = new Thread(ThreadMethod);
thread.Start();
Console.WriteLine("代码执行完成");
//线程方法定义
public static void ThreadMethod()
{
Console.WriteLine("当前线程ID:{0},当前线程名称:{1}",
Thread.CurrentThread.ManagedThreadId,
Thread.CurrentThread.Name);
while (true)
{
Console.WriteLine(DateTime.Now);
Thread.Sleep();
}
}
二、创建一个参数传入object类型的线程
public static void Init()
{
//创建一个参数的线程
//ParameterizedThreadStart 指定传入的类型是object
for (int i = ; i < ; i++)
{
Thread thread = new Thread(new ParameterizedThreadStart(ThreadMethod));
object obj = i * ;
thread.Start(obj);
}
}
//定义线程方法
public static void ThreadMethod(object number)
{
int i = (int)number;
while (true)
{
i++;
Console.WriteLine("当前线程ID:{0},number={1}", Thread.CurrentThread.ManagedThreadId, i);
Thread.Sleep();
}
}

三、创建使用对象实例方法,创建多个参数传入情况的线程
public static void Init()
{
//创建多个传入参数的线程
for (int i = ; i < ; i++)
{
Calculator cal = new Calculator(i, i * );
Thread thread = new Thread(new ThreadStart(cal.Add));
thread.Start();
}
}
public class Calculator
{
public int X { get; set; }
public int Y { get; set; }
public Calculator(int x, int y)
{
this.X = x;
this.Y = y;
}
//定义线程执行方法
public void Add()
{
int i = ;
while (i < )
{
i++;
Console.WriteLine("当前线程ID:{0},{1}+{2}={3}", Thread.CurrentThread.ManagedThreadId, X, Y, X + Y);
Thread.Sleep();
}
}
}

C#中创建线程,创建带参数的线程的更多相关文章
- C#线程调用带参数的方法
在 .NET Framework 2.0 版中,要实现线程调用带参数的方法有两种办法.第一种:使用ParameterizedThreadStart.调用 System.Threading.Thread ...
- Java带参数的线程类ParameterizedThread——即如何给Thread传递参数
在Java中似乎没有提供带运行参数的线程实现类,在第三方类库中也没有找到.网上有大量的文章在讨论这个问题,但都没有提供很好的代码封装解决方案,这令我很吃惊.如果读者知道有官方或者第三方的实现方式,欢迎 ...
- c#创建带参数的线程
1.无参数线程的创建 Thread thread = new Thread(new ThreadStart(getpic)); thread.Start(); private void showmes ...
- JS中的setInterval 函数体带参数f方法
1.setInterval(function code,delaytime); 在设置自动调用执行function code时,我们可以采用下面三种方式来解决. 一.采用字符串形式:(参数不能被周期性 ...
- laravel 中的 toSql 获取带参数的 sql 语句
默认情况下,toSql 获取到的 sql 里面的参数使用 "?" 代替的,如下: DB::table('user')->where('id', 1)->toSql(); ...
- C#线程调用带参数的方法,给控件赋值
System.Threading.Thread thread = new System.Threading.Thread(() => { //各种业务 //定义一个委托 public deleg ...
- Net线程足迹 传递参数至线程
方法一:应用ParameterizedThreadStart这个委托来传递输入参数,这种方法适用于传递单个参数的情况. using System; using System.Collections.G ...
- 在Django中使用ORM创建图书管理系统
一.ORM(对象关系映射) 很多语言的web框架中都有这个概念 1. 为什么要有ORM? 1. 写程序离不开数据,要使用数据就需要连接数据库,但是不同的数据库在sql语句上(mysql,oracle等 ...
- ThreadStart中如何带参数
1.ThreadStart 线程执行带参数的方法,new Thread(new ThreadStart(delegate { ThreadTask(firstPage, lastPage); })); ...
随机推荐
- Adobe Photoshop CS或者CC卸载不了怎么办?
有木有没有遇到这个问题的同学?使用Adobe Creative Suite CleanerToo工具下载就好了~ 下载地址:http://pan.baidu.com/s/1pJ3aBsn
- 转:Ubuntu12.04 LTS 使用心得-开机挂载其他分区
1.在/media目录下创建好用来关联你要挂载的分区的文件夹(相当于一个虚拟目录/挂载点,链接/映射到你要挂载的盘符去) 我要挂载4个分区,所以创建了四个挂载点,名字随便取,只要你自己认的出来哪个对应 ...
- hibernate spring sturts2配置
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...
- BZOJ 1296 粉刷匠
Description windy有\(N\)条木板需要被粉刷.每条木板被分为\(M\)个格子. 每个格子要被刷成红色或蓝色. windy每次粉刷,只能选择一条木板上一段连续的格子,然后涂上一种颜色. ...
- [BZOJ 1085] [SCOI2005] 骑士精神 [ IDA* 搜索 ]
题目链接 : BZOJ 1085 题目分析 : 本题中可能的状态会有 (2^24) * 25 种状态,需要使用优秀的搜索方式和一些优化技巧. 我使用的是 IDA* 搜索,从小到大枚举步数,每次 DFS ...
- All in All
Crawling in process... Crawling failed Description You have devised a new encryption technique which ...
- iOS应用架构浅谈
(整理至http://www.cocoachina.com/ios/20150414/11557.html) 缘由 从事iOS工作一年多了,主要从事QQ钱包SDK开发和财付通app维护,随着对业务的慢 ...
- 字符串[未AC](后缀自动机):HEOI 2016 str
超级恶心,先后用set维护right,再用主席树维护,全部超时,本地测是AC的.放心,BZOJ上还是1S限制,貌似只有常数优化到一定境界的人才能AC吧. 总之我是精神胜利了哦耶QAQ #include ...
- 动态规划——J 括号配对问题
J - 括号匹配 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- cf602B Approximating a Constant Range
B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes ...