需要用timer定时检索数据库并发信,发信动作很耗时间。
在不用timer,直接用测试按钮激活检测数据库并发信的时候,按了按钮以后,程序一定要完成所有动作后才能继续有其他相应,不知道如果能够在按了按钮后,能够用多线程的方式在后台运行这些发送动作。
对应用程序不熟悉,请给示例代码。
另外,定期检测的话,用form.timer还是thread.timer
在不用timer,直接用测试按钮激活检测数据库并发信的时候,按了按钮以后,程序一定要完成所有动作后才能继续有其他相应,不知道如果能够在按了按钮后,能够用多线程的方式在后台运行这些发送动作。
对应用程序不熟悉,请给示例代码。
另外,定期检测的话,用form.timer还是thread.timer
解决方案 »
- 有人用过c#连接hsql数据库吗?知道的人帮我解释一下,谢谢!
- 求助:如何使用c#编辑SVG啊?(急!!)
- mobile:Command 如何设置成不可用
- 判断某个字符在集合有现成的函数吗
- 绑定出错
- 一个关于web browser的简单问题
- 装VS2005时的问题!
- 再次求!!!一个跟时间字段有关的SQL语句,帮忙看看!!!!!
- C#中如何把string类型的9:00 a或者12:00 p转换成DateTime类型的9:00和23:00
- 求思路,我怎么把以下的htm代码中的table输出一个Excel文件
- c#winform程序,数据库有一个字段的值为0和1,0代表男,1代表女,如何在datagrid中把0或1显示为男和女,谢谢
- 求一多线程操作数据库的简单例子
Threading.Timer才是多线程的
th.Start();//作业过程
private void thProc()
{
while(bContinue)
{
//这里从数据库取数据
//其它代码
System.Threading.Thead.Sleep(500);//设置轮询间隔
}
}3)停止任务
bContinue = false;
timer还是需要的,button我只是用来做测试用的。谢谢你的代码,应该用threading类就可以,我先试试看。
msdn的关于threading.timer的示例
using System;
using System.Threading;class TimerExample
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker(10); // Create the delegate that invokes methods for the timer.
TimerCallback timerDelegate =
new TimerCallback(statusChecker.CheckStatus); // Create a timer that signals the delegate to invoke
// CheckStatus after one second, and every 1/4 second
// thereafter.
Console.WriteLine("{0} Creating timer.\n",
DateTime.Now.ToString("h:mm:ss.fff"));
Timer stateTimer =
new Timer(timerDelegate, autoEvent, 1000, 250); // When autoEvent signals, change the period to every
// 1/2 second.
autoEvent.WaitOne(5000, false);
stateTimer.Change(0, 500);
Console.WriteLine("\nChanging period.\n"); // When autoEvent signals the second time, dispose of
// the timer.
autoEvent.WaitOne(5000, false);
stateTimer.Dispose();
Console.WriteLine("\nDestroying timer.");
}
}threading.timer 参数说明
Timer构造函数参数说明:Callback:一个 TimerCallback 委托,表示要执行的方法。
State:一个包含回调方法要使用的信息的对象,或者为空引用(Visual Basic 中为 Nothing)。
dueTime:调用 callback 之前延迟的时间量(以毫秒为单位)。指定 Timeout.Infinite 以防止计时器开始计时。指定零 (0) 以立即启动计时器。
Period:调用 callback 的时间间隔(以毫秒为单位)。指定 Timeout.Infinite 可以禁用定期终止。参考:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingtimerclasstopic.asphttp://www.cnblogs.com/rickie/archive/2004/11/20/66152.html