日期:2014-05-17  浏览次数:21048 次

求c# winform 线程 定时扫描数据库
小弟初接触winform ,对线程还不了解,现在要想1分钟扫描一次数据库.具体怎样实现哟,麻烦给个思路,或者给个线程示例.

------解决方案--------------------
C# code

        private void Form1_Load(object sender, EventArgs e)
        {
            while (true)
            {
                Thread t = new Thread(new ThreadStart(test));
                t.Start();
                Thread.Sleep(1000);
            }
        }

        private void test()
        {
            MessageBox.Show("hello1");
        }

------解决方案--------------------
C# code

        pirvate bool bStop = false;
        private void Form1_Load(object sender, EventArgs e)
        {
                Thread td = new Thread(new ThreadStart(test));
                td.Start();
        }

        private void test()
        {
            while(!bStop)
            {
            //这边进行你的扫描数据库操作
              Thread.Sleep(60 * 1000);
              }
        }

        private void stop()
        {
            bStop = true;
        }

------解决方案--------------------
探讨
引用:
C# codeprivatevoid Form1_Load(object sender, EventArgs e)
        {while (true)
            {
                Thread t=new Thread(new ThreadStart(test));
                t.Start();
                Thread.Sleep(1000);
            }
        }privatevoid test()
        {
            MessageBox.Show("hello1");
        }

或者用 TIMER


我在加载窗体时执行,为什么窗体都显不出来也

------解决方案--------------------
楼上的那些代码会造成窗体假死....

C# code

 private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(AutoLoad));
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
            }
        }

private void AutoLoad(object ob)
        {
            while(true)
            {
                //do...
                Thread.Sleep(1000*60);
            }
        }