线程操作主要用到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#中创建线程,创建带参数的线程的更多相关文章

  1. C#线程调用带参数的方法

    在 .NET Framework 2.0 版中,要实现线程调用带参数的方法有两种办法.第一种:使用ParameterizedThreadStart.调用 System.Threading.Thread ...

  2. Java带参数的线程类ParameterizedThread——即如何给Thread传递参数

    在Java中似乎没有提供带运行参数的线程实现类,在第三方类库中也没有找到.网上有大量的文章在讨论这个问题,但都没有提供很好的代码封装解决方案,这令我很吃惊.如果读者知道有官方或者第三方的实现方式,欢迎 ...

  3. c#创建带参数的线程

    1.无参数线程的创建 Thread thread = new Thread(new ThreadStart(getpic)); thread.Start(); private void showmes ...

  4. JS中的setInterval 函数体带参数f方法

    1.setInterval(function code,delaytime); 在设置自动调用执行function code时,我们可以采用下面三种方式来解决. 一.采用字符串形式:(参数不能被周期性 ...

  5. laravel 中的 toSql 获取带参数的 sql 语句

    默认情况下,toSql 获取到的 sql 里面的参数使用 "?" 代替的,如下: DB::table('user')->where('id', 1)->toSql(); ...

  6. C#线程调用带参数的方法,给控件赋值

    System.Threading.Thread thread = new System.Threading.Thread(() => { //各种业务 //定义一个委托 public deleg ...

  7. Net线程足迹 传递参数至线程

    方法一:应用ParameterizedThreadStart这个委托来传递输入参数,这种方法适用于传递单个参数的情况. using System; using System.Collections.G ...

  8. 在Django中使用ORM创建图书管理系统

    一.ORM(对象关系映射) 很多语言的web框架中都有这个概念 1. 为什么要有ORM? 1. 写程序离不开数据,要使用数据就需要连接数据库,但是不同的数据库在sql语句上(mysql,oracle等 ...

  9. ThreadStart中如何带参数

    1.ThreadStart 线程执行带参数的方法,new Thread(new ThreadStart(delegate { ThreadTask(firstPage, lastPage); })); ...

随机推荐

  1. Apache虚拟站点配置

    简单虚拟站点配置: <VirtualHost 127.0.0.2:80> DocumentRoot E:/wamp/www/yue ServerName 127.0.0.2:80</ ...

  2. 关于C#匿名方法

    作者  陈嘉栋(慕容小匹夫) 阅读目录 0x00 前言 0x01 不必构造委托对象 0x02 匿名方法初探 0x03 使用匿名方法省略参数 0x04 匿名方法和闭包 0x05 匿名方法如何捕获外部变量 ...

  3. [BZOJ 3207] 花神的嘲讽计划Ⅰ【Hash + 可持久化线段树】

    题目链接:BZOJ - 3207 题目分析 先使用Hash,把每个长度为 k 的序列转为一个整数,然后题目就转化为了询问某个区间内有没有整数 x . 这一步可以使用可持久化线段树来做,虽然感觉可以有更 ...

  4. WebKit JavaScript Binding添加新DOM对象的三种方式

    一.基础知识 首先WebKit IDL并非完全遵循Web IDL,只是借鉴使用.WebKit官网提供了一份说明(WebKitIDL),比如Web IDL称"operation”(操作), 而 ...

  5. CELL_PHOTO_IDENTIFIER

    # define CELL_PHOTO_IDENTIFIER @"photoLibraryCell" # define CLOSE_PHOTO_IMAGE @"close ...

  6. Gnome Tetravex

    zoj1008:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008 题目意思是有一个游戏,即给出一个图,该图是由n*n个 ...

  7. IHS怎么通过80端口连接WAS——<转>

    IHS如何通过80端口连接WAS? 经常看到有朋友问这个问题,所以简单总结一下基本步骤:   IHS和WAS之间是通过Plugin来实现的,一般的Plugins和IHS安装在同一物理机器上,WAS安装 ...

  8. (转载)mysql查询一天,查询一周,查询一个月的数据

    (转载)http://www.cnblogs.com/likwo/archive/2010/04/16/1713282.html 查询一天: select * from table where to_ ...

  9. 【贪心】HDU 5783 Divide the Sequence

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5783 题目大意: 把一个N个数的数列拆成若干段,保证每一段的前缀和都非负,求最多能拆成多少段. 题目 ...

  10. 部署ASP.Net项目 遇到总是启用目录浏览或者报HTTP 错误 403.14 - Forbidden 的原因

    部署Asp.Net 网站总是报下面的问题 原因: 没有为网站指定默认文档,增加默认文档 1.选中“默认文档” 2.点击右边“打开功能” 点击右边“添加”按钮,把你想作为的默认页面添加就可以了,重启服务 ...