从子线程返回值
来源:互联网 发布:java工程部署到tomcat 编辑:程序博客网 时间:2024/06/06 01:14
使用回调方法检索数据
下面的示例演示了一个从线程中检索数据的回调方法。包含数据和线程方法的类的构造函数也接受代表回调方法的委托;在线程方法结束前,它调用该回调委托。
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);
}
}
- 从子线程返回值
- python从子线程中获得返回值
- 异步接收子线程的返回值
- python获得子线程的返回值
- python获得子线程的返回值
- java 多线程 线程返回值 子线程超时时间设置
- c#多线程如何从线程返回值
- 从线程返回信息
- 子线程结束后返回值给主线程
- ExecutorService子线程返回值获取和exception捕获
- 取得子线程的返回值(Future 搭载Callable)
- 使用callable获取子线程的返回值
- Java 从线程返回信息
- Java 从线程返回数据
- 线程之从线程返回信息
- 从线程中产生返回值--Callable接口
- java后端实现返回子线程进度条
- 从子 Activity 获取返回结果
- PXE系列之四:PXE无盘LINUX工作站
- 计算年龄的sql语句
- cmake,make,nmake的用法
- 【转】电磁干扰(EMI)问题诊断步骤|电磁兼容(EMC)整改步骤
- CString::CompareNoCase与CString::Compare的区别
- 从子线程返回值
- java设计模式总结
- 对UML几种对象关系的理解
- 使用 ADO.NET 访问 Oracle 9i 存储过程
- 关于ELF文件格式的实验
- 重定位
- 使用客户端脚本
- 大脑练习
- [32位汇编系列]005 - 定时器的使用(1)