`

C# 多线程返回值

 
阅读更多

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 value;

// The constructor obtains the state information.
public ThreadWithState(string text, int number) {
boilerplate = text;
value = number;
}

// The thread procedure performs the task, such as formatting
// and printing a document.
public void ThreadProc() {
Console.WriteLine(boilerplate, value);
}
}

// 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.");
}
}
用回调方法检索数据
下面的示例演示了一个从线程中检索数据的回调方法。包含数据和线程方法的类的构造函数也接受代表回调方法的委托;在线程方法结束前,它调用该回调委托。
using System;
using System.Threading;

// The ThreadWithState class contains the information needed for
// a task, the method that executes the task, and a delegate
// to call when the task is complete.
//
public class ThreadWithState {
// State information used in the task.
private string boilerplate;
private int value;
// Delegate used to execute the callback method when the
// task is complete.
private ExampleCallback callback;

// The constructor obtains the state information and the
// callback delegate.
public ThreadWithState(string text, int number,
ExampleCallback callbackDelegate)
{
boilerplate = text;
value = number;
callback = callbackDelegate;
}

// The thread procedure performs the task, such as
// formatting and printing a document, and then invokes
// the callback delegate with the number of lines printed.
public void ThreadProc() {
Console.WriteLine(boilerplate, value);
if (callback != null)
callback(1);
}
}

// Delegate that defines the signature for the callback method.
//
public delegate void ExampleCallback(int lineCount);

// 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,
new ExampleCallback(ResultCallback)
);

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 callback method must match the signature of the
// callback delegate.
//
public static void ResultCallback(int lineCount) {
Console.WriteLine("Independent task printed {0} lines.", lineCount);
}
}

分享到:
评论

相关推荐

    C#多线程委托带参数

    *********************************************************...两个线程,利用委托更新主线程listBox1控件的内容,并将线程的名字传递过去 ***********************************************************************

    C#多线程函数如何传参数和返回值[归类].pdf

    C#多线程函数如何传参数和返回值[归类].pdf

    多线程,Delegate 委托带参数的方法

    Delegate '委托,带参数的方法,多线程,可以轻松在线程中传递参数,获取返回值.

    C#多线程参数传递

    1、通过实体类来传递(可以传递多个参数与获取返回值),demo如下:  需要在线程中调用的函数: namespace ThreadParameterDemo { public class FunctionClass { public static string TestFunction(string ...

    c# .net 的多线程网站异常监控以及服务器响应速度监控Console源代码

    临时写的,没咋完善,合适于新手用来提升,里面的c# 多线程简单应用,以及定义对象返回值提高通用性,过程日志防并发锁定写入,使用配置文件,可以随意配置运行以及间隔时间,以及xml配置监控站点信息。结构以主程序...

    c#线程Thread示例

    C#是一门支持多线程的语言,因此线程的使用也是比较常见的。由于线程的知识在Win32编程的时候已经说得过多,所以在.Net中很少介绍这部分(可能.Net不觉得这部分是它所特有的)。那么线程相关的问题大致有如下四类...

    c#多线程的应用全面解析

    1.使用多线程的几种方式 (1)不需要传递参数,也不需要返回参数 ThreadStart是一个委托,这个委托的定义为void ThreadStart(),没有参数与返回值。 代码如下:class Program  {  static void Main(string[] args)  {...

    c#进行项目设计时可以直接使用资源

    4.c#基础多线程 5.c#基础修饰符访问返回值 6.c#基础重载 7.c#基础递归 8.c#基础数组 9.c#基础引用 10.c#基础引用 - 作业 11.c#基础ref和out 12.c#基础二分查找法 13.c#基础数组选择排序 14.c#基础数组冒泡排序 15.c#...

    c#使用多线程的几种方式示例详解

    ThreadStart是一个委托,这个委托的定义为void ThreadStart(),没有参数与返回值。 代码如下:class Program { static void Main(string[] args) { for (int i = 0; i < 30; i++) { ThreadStart threadStart = new...

    C#源码大集合 02(共3卷)

    └ C#源代码第三部分多线程 ├─1-多线程 ├─2-多线程 ├─3-多线程互斥 ├─4-多线程互斥 ├─5-多线程互斥 ├─chap09-多线程 ├─D00-多线程 ├─Windows多线程编程技术与实例 ... ... ├─多线程,多接收...

    C#源码大集合 03(共3卷)

    ├─第53讲 文件系统监视器 │ └─第55讲 聊天室扩展之三——使用MS Agent <br>└ C#源代码第三部分多线程 ├─1-多线程 ├─2-多线程 ├─3-多线程互斥 ├─4-多线程互斥 ├─5-多线程...

    深入分析C# Task

    因为由执行工作Task对象通常以异步方式执行线程池线程上而不是以同步方式在主应用程序线程中,可以使用Status属性,并将IsCanceled, IsCompleted,和IsFaulted属性,以确定任务的状态。 大多数情况下,lambda ...

    C#源码大集合 01(共3卷)

    ├─第53讲 文件系统监视器 │ └─第55讲 聊天室扩展之三——使用MS Agent <br>└ C#源代码第三部分多线程 ├─1-多线程 ├─2-多线程 ├─3-多线程互斥 ├─4-多线程互斥 ├─5-多线程...

    在C#中SendMessage和PostMessage的参数传递

    SendMessage 在返回前还做了很多工作,比如,响应别的线程向它 SendMessage。Post 到别的线程时,最好用 PostThreadMessage 代替 PostMessage,PostMessage 的 hWnd 参数可以是 NULL,等效于 PostThreadMessage + ...

    C#5.0本质论第四版(因文件较大传的是百度网盘地址)

    第18章 多线程处理 507 18.1 多线程基础 509 18.2 使用System.Threading 512 18.2.1 使用System.Threading.Thread进行异步操作 512 18.2.2 线程管理 514 18.2.3 在生产代码中不要让线程进入...

    异步多线程调用IAsyncResult

    异步方法执行,使用BeginInvoke (AsyncCallback委托对象) ,而不用返回值

    C#本质论(第3版)

    C#本质论(第3版) 详细介绍C# 4.0 第1章 c#概述 1.1 hello world 1.2 c#语法基础 1.2.1 c#关键字 1.2.2 类型定义 ... 第19章 同步和更多多线程处理模式 第20章 平台互操作性的不安全的代码 第21章 cli

    C#如何在后台捕捉按键

    C#如何在后台捕捉按键 [此问题的推荐答案] API别忘了 using System.Runtime.InteropServices; [DllImport("user32.dll")] public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 ...

    在一小时内学会 C#(txt版本)

    本文特别适合有 C++ 基础却没有太多精力学习 C# 的读者。 关于作者 Aisha Ikram 我现在在英国一家软件公司任技术带头人。我是计算机科学的硕士。我主要使用 .NET 1.1/2.0, C#, VB.NET, ASP.NET, VC++ 6, MFC, ...

    C#全能速查宝典

    《C#全能速查宝典》共分为8章,分别介绍了C#语言基础、Windows窗体及常用控件、Windows高级控件、控件公共属性、方法及事件、数据库开发、文件、数据流与注册表、GDI+绘图技术和C#高级编程,共包含562个C#编程中常用...

Global site tag (gtag.js) - Google Analytics