您的位置:首页 > 其它

多线程例子,传数据|软件开发|转自博客园

2008-12-21 14:27 260 查看
用回调方法检索数据

using System;

using System.Threading;

//被实例化的类,用于传递参数进行操作

public class ThreadWithState

//声明一个回调函数:注意传递的参数要与Example类中的函数参数类型一致

public delegate void ExampleCallback(int lineCount);

//事例主类

public class Example

using System;

using System.Threading;

//被实例化的类,用于传递参数进行操作

public class ThreadWithState

//创建线程调用ThreadWithState

public class Example

{

public static void Main()

{

//实例化类ThreadWithState,利用构造函数向线程传递参数

ThreadWithState tws = new ThreadWithState("This number {0}.", 42);

//创建线程并执行ThreadWithState类中的ThreadProc函数

Thread t = new Thread(new ThreadStart(tws.ThreadProc));

t.Start();

Console.WriteLine("线程开始执行");

//此为判断线程是否执行结束,但不建议阻塞线程,这样线程就会等待完成后再进行其他操作

//可利用:t.IsAlive是否处于执行状态来判断线程

t.Join();

Console.WriteLine("线程执行结束");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: