异步编程(1)

本文介绍C#中线程池、专用线程及定时器的使用方法,包括线程池QueueUserWorkItem方法、专用线程创建及启动、定时器的启动与停止等。并对比了线程池与专用线程的适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们知道,使用多线程可以提高程序运行的效率,加速程序的运行。但是我们也应该知道每个线程都要耗费许多资源,在程序中不是运行的线程越多好,我们要掌握如何充分利用多线程的优势。要尽量使线程运行,不要让它挂起,因为挂起的线程不再运行。但是仍然耗费系统资源。

线程池是一种非常好的技术,可以大大提高程序的效率,而且又把新建每个线程的消耗降到最小。下面我们看一下c#中提供的有关线程池的方法以及如何使用:

static Boolean QueueUserWorkItem(WaitCallback callBack);
static Boolean QueueUserWorkItem(WaitCallback callBack, Object state);
static Boolean UnsafeQueueUserWorkItem(WaitCallback callBack, Object state);


using System;
using System.Threading;
public static class Program {
public static void Main() {
Console.WriteLine(
"Main thread: queuing an asynchronous operation");

ThreadPool.QueueUserWorkItem(ComputeBoundOp, 
5);
Console.WriteLine(
"Main thread: Doing other work here...");
Thread.Sleep(
10000); 
Console.WriteLine(
"Hit <Enter> to end this program...");
Console.ReadLine();

}


private static void ComputeBoundOp(Object state) {
Console.WriteLine(
"In ComputeBoundOp: state={0}", state);
Thread.Sleep(
1000); 
}

}


//有时候是这样的结果
Main thread: queuing an asynchronous operation
Main thread: Doing other work here...
In ComputeBoundOp: state
=5
//有时候又是这样的结果
Main thread: queuing an asynchronous operation
In ComputeBoundOp: state
=5
Main thread: Doing other work here...

//我们看出两个线程的调度是由系统决定的,我们是无法选择的。

 下面再来看一下使用专用线程,我们知道如果程序只需要其他的1~2线程来辅助程序运行,这种情况下用线程池恐怕不是很适合,而使用专用线程比较好:

using System;
using System.Threading;
public static class Program {
public static void Main() {
Console.WriteLine(
"Main thread: starting a dedicated thread " +"to do an asynchronous operation");

Thread dedicatedThread 
= new Thread(ComputeBoundOp);
dedicatedThread.Start(
5);

Console.WriteLine(
"Main thread: Doing other work here...");
Thread.Sleep(
10000); 
dedicatedThread.Join();                                                                      
//等待线程终止

Console.WriteLine(
"Hit <Enter> to end this program...");
Console.ReadLine();
}

private static void ComputeBoundOp(Object state) {
Console.WriteLine(
"In ComputeBoundOp: state={0}", state);
Thread.Sleep(
1000); 
}

}


//同样是两种输出结果
Main thread: starting a dedicated thread to do an asynchronous operation
Main thread: Doing other work here...
In ComputeBoundOp: state
=5

Main thread: starting a dedicated thread to 
do an asynchronous operation
In ComputeBoundOp: state
=5
Main thread: Doing other work here...

下面看一下定期执行某种操作我们应该如何使用定时器:

using System;
using System.Threading;
public static class Program {
public static void Main() {
Console.WriteLine(
"Main thread: starting a timer");
Timer t 
= new Timer(ComputeBoundOp, 502000);
Console.WriteLine(
"Main thread: Doing other work here...");
Thread.Sleep(
10000); 
t.Dispose(); 
}


private static void ComputeBoundOp(Object state) {
Console.WriteLine(
"In ComputeBoundOp: state={0}", state);
Thread.Sleep(
1000); // Simulates other work (1 second)
}

}

如果我们查看FCL的话,我们会看到三个Timer类,下面分别介绍一下他们:

System.Threading.Timer                 //这个定时器就是我们使用的那个,是最好的一个定时器,建议都使用这个

System.Windows.Forms.Timer        
/*构建一个该类的实例可以告诉Windows将定时器与调用线程关联。随着定时器的触发,Windows将一个定时消息插入到线程的消息队列中。调用线程必须执行一个消息循环,从而提取消息,并分别将他们分派到希望的回调方法中。*/

System.Timers.Timer
/*该定时器基本上是对System.Threading.Timer   的一个封装,当定时器到期后,经导致FCL将事件加入线程池队列中。这个定时器有可能被移除,以便都使用System.Threading.Timer */
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值