本文转载自:http://www.jb51.net/article/46234.htm

本文章主要介绍了c#使用多线程的几种方式,通过示例学习c#的多线程使用方式,大家参考使用吧

(1)不需要传递参数,也不需要返回参数

ThreadStart是一个委托,这个委托的定义为void ThreadStart(),没有参数与返回值。

复制代码 代码如下:

class Program

{

static void Main(string[] args)

{

for (int i = 0; i < 30; i++)

{

ThreadStart threadStart = new ThreadStart(Calculate);

Thread thread = new Thread(threadStart);

thread.Start();

}

Thread.Sleep(2000);

Console.Read();

}

public static void Calculate()

{

DateTime time = DateTime.Now;//得到当前时间

Random ra = new Random();//随机数对象

Thread.Sleep(ra.Next(10,100));//随机休眠一段时间

Console.WriteLine(time.Minute + ":" + time.Millisecond);

}

}

(2)需要传递单个参数

ParameterThreadStart委托定义为void ParameterizedThreadStart(object state),有一个参数但是没有返回值。

复制代码 代码如下:

class Program

{

static void Main(string[] args)

{

for (int i = 0; i < 30; i++)

{

ParameterizedThreadStart tStart = new ParameterizedThreadStart(Calculate);

Thread thread = new Thread(tStart);

thread.Start(i*10+10);//传递参数

}

Thread.Sleep(2000);

Console.Read();

}

public static void Calculate(object arg)

{

Random ra = new Random();//随机数对象

Thread.Sleep(ra.Next(10, 100));//随机休眠一段时间

Console.WriteLine(arg);

}

}

(3)使用专门的线程类(常用)

使用线程类可以有多个参数与多个返回值,十分灵活!

复制代码 代码如下:

class Program

{

static void Main(string[] args)

{

MyThread mt = new MyThread(100);

ThreadStart threadStart = new ThreadStart(mt.Calculate);

Thread thread = new Thread(threadStart);

thread.Start();

//等待线程结束

while (thread.ThreadState != ThreadState.Stopped)

{

Thread.Sleep(10);

}

Console.WriteLine(mt.Result);//打印返回值

Console.Read();

}

}

public class MyThread//线程类

{

public int Parame { set; get; }//参数

public int Result { set; get; }//返回值

//构造函数

public MyThread(int parame)

{

this.Parame = parame;

}

//线程执行方法

public void Calculate()

{

Random ra = new Random();//随机数对象

Thread.Sleep(ra.Next(10, 100));//随机休眠一段时间

Console.WriteLine(this.Parame);

this.Result = this.Parame * ra.Next(10, 100);

}

}

(4)使用匿名方法(常用)

使用匿名方法启动线程可以有多个参数和返回值,而且使用非常方便!

复制代码 代码如下:

class Program

{

static void Main(string[] args)

{

int Parame = 100;//当做参数

int Result = 0;//当做返回值

//匿名方法

ThreadStart threadStart = new ThreadStart(delegate()

{

Random ra = new Random();//随机数对象

Thread.Sleep(ra.Next(10, 100));//随机休眠一段时间

Console.WriteLine(Parame);//输出参数

Result = Parame * ra.Next(10, 100);//计算返回值

});

Thread thread = new Thread(threadStart);

thread.Start();//多线程启动匿名方法

//等待线程结束

while (thread.ThreadState != ThreadState.Stopped)

{

Thread.Sleep(10);

}

Console.WriteLine(Result);//打印返回值

Console.Read();

}
}

(5)使用委托开启多线程(多线程深入)

1、用委托(Delegate)的BeginInvoke和EndInvoke方法操作线程

BeginInvoke方法可以使用线程异步地执行委托所指向的方法。然后通过EndInvoke方法获得方法的返回值(EndInvoke方法的返回值就是被调用方法的返回值),或是确定方法已经被成功调用。

复制代码 代码如下:

class Program

{

private delegate int NewTaskDelegate(int ms);

private static int newTask(int ms)

{

Console.WriteLine("任务开始");

Thread.Sleep(ms);

Random random = new Random();

int n = random.Next(10000);

Console.WriteLine("任务完成");

return n;

}

static void Main(string[] args)

{

NewTaskDelegate task = newTask;

IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);

//EndInvoke方法将被阻塞2秒

int result = task.EndInvoke(asyncResult);

Console.WriteLine(result);

Console.Read();

}

}

2、使用IAsyncResult.IsCompleted属性来判断异步调用是否完成

复制代码 代码如下:

class Program

{

private delegate int NewTaskDelegate(int ms);

private static int newTask(int ms)

{

Console.WriteLine("任务开始");

Thread.Sleep(ms);

Random random = new Random();

int n = random.Next(10000);

Console.WriteLine("任务完成");

return n;

}

static void Main(string[] args)

{

NewTaskDelegate task = newTask;

IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);

//等待异步执行完成

while (!asyncResult.IsCompleted)

{

Console.Write("*");

Thread.Sleep(100);

}

// 由于异步调用已经完成,因此, EndInvoke会立刻返回结果

int result = task.EndInvoke(asyncResult);

Console.WriteLine(result);

Console.Read();

}

}

3、使用WaitOne方法等待异步方法执行完成

WaitOne的第一个参数表示要等待的毫秒数,在指定时间之内,WaitOne方法将一直等待,直到异步调用完成,并发出通知,WaitOne方法才返回true。当等待指定时间之后,异步调用仍未完成,WaitOne方法返回false,如果指定时间为0,表示不等待,如果为-1,表示永远等待,直到异步调用完成。

复制代码 代码如下:

class Program

{

private delegate int NewTaskDelegate(int ms);

private static int newTask(int ms)

{

Console.WriteLine("任务开始");

Thread.Sleep(ms);

Random random = new Random();

int n = random.Next(10000);

Console.WriteLine("任务完成");

return n;

}

static void Main(string[] args)

{

NewTaskDelegate task = newTask;

IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);

//等待异步执行完成

while (!asyncResult.AsyncWaitHandle.WaitOne(100, false))

{

Console.Write("*");

}

int result = task.EndInvoke(asyncResult);

Console.WriteLine(result);

Console.Read();

}

}

4、使用回调方式返回结果

要注意的是“my.BeginInvoke(3,300, MethodCompleted, my)”,BeginInvoke方法的参数传递方式:

前面一部分(3,300)是其委托本身的参数。

倒数第二个参数(MethodCompleted)是回调方法委托类型,他是回调方法的委托,此委托没有返回值,有一个IAsyncResult类型的参数,当method方法执行完后,系统会自动调用MethodCompleted方法。

最后一个参数(my)需要向MethodCompleted方法中传递一些值,一般可以传递被调用方法的委托,这个值可以使用IAsyncResult.AsyncState属性获得。

复制代码 代码如下:

class Program

{

private delegate int MyMethod(int second, int millisecond);

//线程执行方法

private static int method(int second, int millisecond)

{

Console.WriteLine("线程休眠" + (second * 1000 + millisecond) + "毫秒");

Thread.Sleep(second * 1000 + millisecond);

Random random = new Random();

return random.Next(10000);

}

//回调方法

private static void MethodCompleted(IAsyncResult asyncResult)

{

if (asyncResult == null || asyncResult.AsyncState == null)

{

Console.WriteLine("回调失败!!!");

return;

}

int result = (asyncResult.AsyncState as MyMethod).EndInvoke(asyncResult);

Console.WriteLine("任务完成,结果:" + result);

}

static void Main(string[] args)

{

MyMethod my = method;

IAsyncResult asyncResult = my.BeginInvoke(3,300, MethodCompleted, my);

Console.WriteLine("任务开始");

Console.Read();

}

}

5、其他组件的BeginXXX和EndXXX方法

在其他的.net组件中也有类似BeginInvoke和EndInvoke的方法,如System.Net.HttpWebRequest类的BeginGetResponse和EndGetResponse方法。其使用方法类似于委托类型的BeginInvoke和EndInvoke方法,例如:

复制代码 代码如下:

class Program

{

//回调函数

private static void requestCompleted(IAsyncResult asyncResult)

{

if (asyncResult == null || asyncResult.AsyncState==null)

{

Console.WriteLine("回调失败");

return;

}

HttpWebRequest hwr = asyncResult.AsyncState as HttpWebRequest;

HttpWebResponse response = (HttpWebResponse)hwr.EndGetResponse(asyncResult);

StreamReader sr = new StreamReader(response.GetResponseStream());

string str = sr.ReadToEnd();

Console.WriteLine("返回流长度:"+str.Length);

}

static void Main(string[] args)

{

HttpWebRequest request =

(HttpWebRequest)WebRequest.Create("http://www.baidu.com");

//异步请求

IAsyncResult asyncResult = request.BeginGetResponse(requestCompleted, request);

Console.WriteLine("任务开始");

Console.Read();

}

}

c#使用多线程的几种方式示例详解的更多相关文章

  1. python模块的导入的两种方式区别详解

    Python 有两种导入模块的方法.两种都有用,你应该知道什么时候使用哪一种方法.一种方法,import module,另一种是from module import,下面是 from module i ...

  2. 黄聪:异步加载JS的4种方式(详解)

    方案1:$(document).ready <!DOCTYPE html> <html> <head> <script src="http://co ...

  3. Java中数组判断元素存在几种方式比较详解

    1. 通过将数组转换成List,然后使用List中的contains进行判断其是否存在 public static boolean useList(String[] arr,String contai ...

  4. 异步加载JS的4种方式(详解)

    方案1:$(document).ready <!DOCTYPE html> <html> <head> <script src="http://co ...

  5. gcc与g++的编译链接的示例详解

    一.编译方式的示例详解 1. 编译C代码 代码如下:main.c /*!  ************************************************************** ...

  6. Java多线程系列--“基础篇”02之 常用的实现多线程的两种方式

    概要 本章,我们学习“常用的实现多线程的2种方式”:Thread 和 Runnable.之所以说是常用的,是因为通过还可以通过java.util.concurrent包中的线程池来实现多线程.关于线程 ...

  7. java多线程系类:基础篇:02常用的实现多线程的两种方式

    本章,我们学习"常用的实现多线程的2种方式":Thread 和 Runnable.之所以说是常用的,是因为通过还可以通过java.util.concurrent包中的线程池来实现多 ...

  8. Java实现多线程的两种方式

    实现多线程的两种方式: 方式1: 继承Thread类 A: 自定义MyThread类继承Thread类 B: 在MyThread类中重写run() C: 创建MyThread类的对象 D: 启动线程对 ...

  9. c# 多线程的几种方式

    1.什么是线程? 进程作为操作系统执行程序的基本单位,拥有应用程序的资源,进程包含线程,进程的资源被线程共享,线程不拥有资源. 2.前台线程和后台线程的区别? 程序关闭时,后台线程直接关闭,但前台线程 ...

随机推荐

  1. jquery 判断网络资源,网络文件是否存在

    前提是,不能跨域访问引入jquery库<script src="jQuery.js" type="text/javascript"></scr ...

  2. [NHibernate]立即加载

    目录 写在前面 文档与系列文章 立即加载 一个例子 总结 写在前面 上篇文章介绍了nhibernate延迟加载的相关内容,简单回顾一下延迟加载,就是需要的时候再去加载,需要的时候再向数据库发出sql指 ...

  3. socket编程热身程序

    /*server.c*/ #include <stdio.h> #include <arpa/inet.h> #include <sys/types.h> /* S ...

  4. POJ 2478 Farey Sequence

     名字是法雷数列其实是欧拉phi函数              Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  5. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  6. H5案例分享:移动端滑屏 touch事件

    移动端滑屏 touch事件 移动端触屏滑动的效果的效果在电子设备上已经被应用的越来越广泛,类似于PC端的图片轮播,但是在移动设备上,要实现这种轮播的效果,就需要用到核心的touch事件.处理touch ...

  7. linux mingling

    grep 文本搜索工具 -i 忽略大小写 -v 不显示匹配行 -c 显示符合条件的行数值 文本搜索支持正则表达式 1 2 3 cat /etc/passwd | grep root // 显示包含ro ...

  8. SAM/BAM文件处理

    当测序得到的fastq文件map到基因组之后,我们通常会得到一个sam或者bam为扩展名的文件.SAM的全称是sequence alignment/map format.而BAM就是SAM的二进制文件 ...

  9. Spring @AspectJ 实现AOP 入门例子(转)

    AOP的作用这里就不再作说明了,下面开始讲解一个很简单的入门级例子. 引用一个猴子偷桃,守护者守护果园抓住猴子的小情节. 1.猴子偷桃类(普通类): package com.samter.common ...

  10. Redis3.0 配置文件说明

    背景: 以前有篇文章已经结果过了,现在复习一下,对Redis3.0进行说明: 参数说明: #redis.conf # Redis configuration file example. # ./red ...