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();
        }

posted @ 2013-03-27 19:11  louiskoo  阅读(1987)  评论(0编辑  收藏  举报