博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 带参多线程操作
阅读量:3905 次
发布时间:2019-05-23

本文共 5640 字,大约阅读时间需要 18 分钟。

C# 带参多线程操作

启动时创建线程并传递数据

创建线程

新建 Thread 对象会新建托管线程。 Thread 类包含需要使用 ThreadStart 委托或 ParameterizedThreadStart 委托的构造函数;委托包装在调用 Start 方法时由新线程调用的方法。 多次调用 Start 会导致 ThreadStateException 抛出。

线程一旦启动,就无需保留对 Thread 对象的引用。 线程可以继续执行到线程过程结束。
下面的代码示例新建两个线程,以对另一个对象调用实例和静态方法:

using System;using System.Threading;public class ServerClass{
// The method that will be called when the thread is started. public void InstanceMethod() {
Console.WriteLine( "ServerClass.InstanceMethod is running on another thread."); // Pause for a moment to provide a delay to make // threads more apparent. Thread.Sleep(3000); Console.WriteLine( "The instance method called by the worker thread has ended."); } public static void StaticMethod() {
Console.WriteLine( "ServerClass.StaticMethod is running on another thread."); // Pause for a moment to provide a delay to make // threads more apparent. Thread.Sleep(5000); Console.WriteLine( "The static method called by the worker thread has ended."); }}public class Simple{
public static void Main() {
ServerClass serverObject = new ServerClass(); // Create the thread object, passing in the // serverObject.InstanceMethod method using a // ThreadStart delegate. Thread InstanceCaller = new Thread( new ThreadStart(serverObject.InstanceMethod)); // Start the thread. InstanceCaller.Start(); Console.WriteLine("The Main() thread calls this after " + "starting the new InstanceCaller thread."); // Create the thread object, passing in the // serverObject.StaticMethod method using a // ThreadStart delegate. Thread StaticCaller = new Thread( new ThreadStart(ServerClass.StaticMethod)); // Start the thread. StaticCaller.Start(); Console.WriteLine("The Main() thread calls this after " + "starting the new StaticCaller thread."); }}// The example displays the output like the following:// The Main() thread calls this after starting the new InstanceCaller thread.// The Main() thread calls this after starting the new StaticCaller thread.// ServerClass.StaticMethod is running on another thread.// ServerClass.InstanceMethod is running on another thread.// The instance method called by the worker thread has ended.// The static method called by the worker thread has ended.

将数据传递到线程

将线程过程和数据封装到帮助程序类中,并使用 ThreadStart 委托执行线程过程。 下面的示例演示这一方法:

using System;using System.Threading;// The ThreadWithState class contains the information needed for// a task, and the method that executes the task.//public class ThreadWithState{
// State information used in the task. private string boilerplate; private int numberValue; // The constructor obtains the state information. public ThreadWithState(string text, int number) {
boilerplate = text; numberValue = number; } // The thread procedure performs the task, such as formatting // and printing a document. public void ThreadProc() {
Console.WriteLine(boilerplate, numberValue); }}// Entry point for the example.//public class Example{
public static void Main() {
// Supply the state information required by the task. ThreadWithState tws = new ThreadWithState( "This report displays the number {0}.", 42); // Create a thread to execute the task, and then // start the thread. Thread t = new Thread(new ThreadStart(tws.ThreadProc)); t.Start(); Console.WriteLine("Main thread does some work, then waits."); t.Join(); Console.WriteLine( "Independent task has completed; main thread ends."); }}// The example displays the following output:// Main thread does some work, then waits.// This report displays the number 42.// Independent task has completed; main thread ends.

多线程操作

下面的例程演示的是所有子线程并发执行,并且所有子线程都执行完任务后,主线程才能接着执行后面的任务。其中class Decode创建并启动多个子线程,子线程要执行的任务由class WriteDataToList定义

public class Decode{
List
threads = new List
(); for (int threadCount = 0; threadCount < threadNum; threadCount++) {
WriteDataToList threadId = new WriteDataToList(decodeData, threadCount,dotNum,threadNum ,decodeLength, currentLocation); threads.Add(new Thread(new ThreadStart(threadId.ThreadProc))); } foreach (var thread in threads) {
thread.Start(); } //等待所有子线程结束后再运行主线程 foreach (var thread in threads) {
thread.Join(); }}
public class WriteDataToList{
int currentLocation; private byte[] decodeData; private int threadId; public WriteDataToList(byte[] buffer, int id, int dotNumber,int threadNumber ,int dataLength,int location) {
currentLocation = location; decodeData = buffer; threadId = id; } public void ThreadProc() {
//do something Debug.Log(threadId); }}

下面的例子演示的是每次只允许一个子线程执行任务,其他线程等待。这样做的目的是为了方便多线程的调试:

class Decode{
List
threads = new List
(); for (int threadCount = 0; threadCount < threadNum; threadCount++) {
WriteDataToList threadId = new WriteDataToList(decodeData, threadCount,dotNum,threadNum ,decodeLength, currentLocation); threads.Add(new Thread(new ThreadStart(threadId.ThreadProc))); } //每次只允许一个子线程参与执行 foreach (var thread in threads) {
thread.Start(); thread.Join(); }}

参考链接

转载地址:http://bloen.baihongyu.com/

你可能感兴趣的文章
通配符与正则表达式
查看>>
c++ 与 Java 之 红黑树 哈希表 辨析
查看>>
open GL 、DirectX、open CV、 open Inventor 、cocos2dx、unity3d、3dmax辨析
查看>>
理解矩阵
查看>>
彩虹七色的RGB值
查看>>
常用正则表达式实例
查看>>
java之面向对象——继承、封装、多态
查看>>
web网站架构演变过程
查看>>
c、 c++、 java 基本数据类型 对比辨析
查看>>
文件系统之 簇&块
查看>>
Android 音乐播放器 源码 下载 高仿魅族系统音乐播放器
查看>>
ERP生产线管理系统 with WAMP
查看>>
深度学习DeepLearning推荐论文清单 初学者入门推荐学习路线
查看>>
Ubuntu安装tensorflow报错:tensorflow-xx.whl not a supported wheel on this platform
查看>>
本证方程的本征值 与 特征方程的特征值 的 区别 辨析
查看>>
基于Android 精简版 WiFi 聊天 源码下载
查看>>
基于Android 精简版 Bluetooth 蓝牙 聊天 源码下载
查看>>
path与classpath区别
查看>>
bat文件·启动程序&打开文件夹
查看>>
java 网络爬虫jsoup 抓取全中国 省市县镇村 完整全集信息 代码
查看>>