使用Thread包装类进行多线程操作
来源:互联网 发布:网络摄像头监控偷拍 编辑:程序博客网 时间:2024/04/29 16:39
Java中使用接口的方式,实现方法的回调机制。而在.NET里,我们通过代理,能够很轻松的实现方法的回调。
class Class1
{
[STAThread]
static void Main(string[] args)
{
Class1 c = new Class1();
ThreadWrapper tw = new ThreadWrapper(new Callback(c.PrintResult), 100);
// 线程开始
tw.Start();
Console.ReadLine();
}
// 打印结果,用于回调
public void PrintResult(int result)
{
Console.WriteLine("Result: " + result.ToString());
}
}
// 回调代理
public delegate void Callback(int n);
class ThreadWrapper
{
public readonly Thread mThread;
private Callback callback;
private int n;
// 参数1:用于回调的代理
// 参数2:需要计算的值
public ThreadWrapper(Callback callback, int arg)
{
this.callback = callback;
ThreadStart myThreadDelegate = new ThreadStart(this.Run);
this.mThread = new Thread(myThreadDelegate);
this.n = arg;
}
// 线程的对外接口
public void Start()
{
this.mThread.Start();
}
// 需要在线程里调用的方法
public void Run()
{
int result = this.Caculate(this.n);
callback(result);
}
private int Caculate(int n)
{
return n*100;
}
}
- 使用Thread包装类进行多线程操作
- C#多线程的使用(Thread)-多线程的挂起,恢复操作
- 使用C++11进行多线程归并排序:std::thread
- C#中的多线程使用 -- Thread 类
- C++使用thread类多线程编程
- C#中的多线程使用 -- Thread 类
- C#中的多线程使用 -- Thread 类
- C++使用thread类多线程编程
- Java Thread 多线程 操作线程
- Java Thread 多线程 操作线程
- Java Thread 多线程 操作线程
- Python3 类包装实现多线程
- asynctask 异步线程类进行多线程操作
- 使用包装模式进行GZIP数据压缩过滤
- Thread类(Java多线程操作之Thread类,源码解析)
- 多线程Thread类创建多线程
- [Python]多线程入门 --thread使用
- java多线程使用(Thread)
- Using the Debug Class
- 初涉Rails
- Conditional Compilation
- J2ME开发网
- Summary
- 使用Thread包装类进行多线程操作
- 安家
- 花之解语
- 推荐几个.NET开源图表组件
- C++中的auto_ptr (修改版本)
- java中的 by value or by reference
- 关于asp中多条记录提交的问题?
- 20050909
- read-Atleap-10-主业务分析-ContentField类-Hibernate多对一关系实施案例