需要用timer定时检索数据库并发信,发信动作很耗时间。
在不用timer,直接用测试按钮激活检测数据库并发信的时候,按了按钮以后,程序一定要完成所有动作后才能继续有其他相应,不知道如果能够在按了按钮后,能够用多线程的方式在后台运行这些发送动作。
对应用程序不熟悉,请给示例代码。
另外,定期检测的话,用form.timer还是thread.timer 

解决方案 »

  1.   

    Forms.Timer不符合你的要求 它是在UI线程里执行的
    Threading.Timer才是多线程的
      

  2.   

    用线程可以,可以不用timer1)先定义一个全局变量,指示是否轮询public bool bContinue = true; 2) 在button1_click加入事件,启动下面线程Thread th = new Thread(new ThreadStart(thProc));
    th.Start();//作业过程
    private void thProc()
    {
    while(bContinue)
    {
        //这里从数据库取数据
        //其它代码
        System.Threading.Thead.Sleep(500);//设置轮询间隔
    }
    }3)停止任务
      bContinue = false;
      

  3.   

    to weisunding(鼎鼎) 
    timer还是需要的,button我只是用来做测试用的。谢谢你的代码,应该用threading类就可以,我先试试看。
      

  4.   

    关于基本的线程和线程.timer的问题已经解决,谢谢各位
    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