//timer 开辟新线程,不占用主线程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了//timer 开辟新线程,不占用主线程相关的知识,希望对你有一定的参考价值。

  private void timer1_Tick_1(object sender, EventArgs e)
        {

           //开辟新线程,不占用主线程
            Thread t = new Thread(new ThreadStart(delegate
            {
                timer1.Stop();
                while (true)
                {
                    Console.WriteLine("in timer1_Tick_1");
                    if (result.Count >= number)
                    {
                        Console.WriteLine("result.Count = " + result.Count);
                        Console.WriteLine("start saving");
                        for (int i = 0; i < result.Count; i++)
                        {
                            dt.AirwayTables.InsertOnSubmit(result[i]);
                            //Console.WriteLine(result[i].TimeTick + " has benn saved!");
                        }
                        dt.SubmitChanges();
                        Console.WriteLine("Saved");
                        result = new List<AirwayTable>();
                    }
                    Application.DoEvents();
                    Console.WriteLine("out timer1_Tick_1");
                    Thread.Sleep(1000);
                }
            }));
            t.Start();
        }

 

http://www.cnblogs.com/liuxinls/archive/2013/03/27/2985215.html

以上是关于//timer 开辟新线程,不占用主线程的主要内容,如果未能解决你的问题,请参考以下文章

c# 多线程运行时出现主界面卡死现象如何解决?

C ++`Timer`类实现

在非主线程里面使用NSTimer创建和取消定时任务

解决定时器在主线程不工作问题

进程线程

Delphi 线程Timer (TThreadTimer)