winform程序  我用一个timer控件 每200MS执行一次  用来获取数据  当获取到数据的时候  会弹出新窗体  但是弹出的新窗体会卡死   
在不停用该timer控件的情况下  怎么优化其性能 (试过用线程  但好像也有这样的问题  不知道是不是我线程没写好)    c#winform线程优化

解决方案 »

  1.   

    BeginInvoke  委托开启新窗体!
      

  2.   

    http://www.cnblogs.com/Zeech-Lee/archive/2011/10/14/2212376.html
      

  3.   

    你timer线程中创建的窗体肯定没时间响应你的动作,因为线程都一直在工作。正确做法是在主窗口中提供一个方法用于调用委托显示新窗体。
      

  4.   


    private void ShowMsg(string msg)
            {
                if (this.InvokeRequired)
                {
                    Action<string, bool> at = ShowMsg;
                    this.Invoke(at, msg);
                }
                else
                {
                    //显示窗口代码
                    new FormMsg(msg).Show();
                }
            }
      

  5.   

    代码考过来没改完,改成:private void ShowMsg(string msg)
            {
                if (this.InvokeRequired)
                {
                    Action<string> at = ShowMsg;
                    this.Invoke(at, msg);
                }
                else
                {
                    //显示窗口代码
                    new FormMsg(msg).Show();
                }
            }
      

  6.   

     private void timer1_Tick(object sender, EventArgs e)
            {            Phone ph = new Phone();
                AddPhone(ph);
                if (first == 0)
                {
                    GetPhone_RunningEvent(null);
                    first++;
                }
            }        public void AddPhone(Phone ph)
            {
                try
                {
                    StringBuilder str = new StringBuilder();
                    int count = LClient.LClientRcv(str);
                    if (!string.IsNullOrWhiteSpace(str.ToString()))
                    {
                        ph = PhoneCon(str.ToString());
                        if (ph.Number != null)
                        {
                            waitPhone.Add(ph);
                            this.BeginInvoke(new BeginInvokeCreateForm(GetPhone_RunningEvent));
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            } public void GetPhone_RunningEvent(object sender)
            {
                if (Jump.Ph == null)
                {
                    if (waitPhone.Count > 0)
                    {
                        if (string.IsNullOrEmpty(waitPhone[0].Error))
                        {
                            Jump.Ph = waitPhone[0]; //打开新窗体  该窗体关闭时触发GetPhone_RunningEvent事件
                            Jump.OpenJp().Show();
                            waitPhone.RemoveAt(0);
                        }
                        else
                        {
                            waitPhone.RemoveAt(0);
                            GetPhone_RunningEvent(sender);
                        }
                    }
                }
                RunningEvent -= GetPhone_RunningEvent;
            }
      

  7.   

    如果没有应用到其他主线程东西,你就在线程直接new Form().Show()也是可以的
      

  8.   

    就timer控件在执行  帮忙看看代码  在8楼  
      

  9.   

    timer过于频繁,主线程又会占着,你就改成Thread线程去处理
    放到Load事件启动
    Thread _th=new Thread(new ThreadStart(Helper));
    _th.Start();void Helper()
    {
        while(!isBreak)
       {
        Phone ph = new Phone();
                AddPhone(ph);
                if (first == 0)
                {
                    GetPhone_RunningEvent(null);
                    first++;
                }
    Thread.Sleep(200);//每200MS一次
    }
    }public void AddPhone(Phone ph)
            {
                try
                {
                    StringBuilder str = new StringBuilder();
                    int count = LClient.LClientRcv(str);
                    if (!string.IsNullOrWhiteSpace(str.ToString()))
                    {
                        ph = PhoneCon(str.ToString());
                        if (ph.Number != null)
                        {
                            waitPhone.Add(ph);
                            //新窗口,直接弹出不用委托
                           // this.BeginInvoke(new BeginInvokeCreateForm(GetPhone_RunningEvent));
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
      

  10.   

    那你就在定时器里加Appliction.DoEvents()
      

  11.   

    在弹出新窗体之前把timer stop()一下,处理完了重新start()
      

  12.   

    timer弹出窗体肯定是没问题的,你用个循环messagebox.show()是可以无限弹出的。
    只是想不通你为什么要这么设计呢?