C# 死循环导致界面卡死问题

hehehndy 2013-09-10 03:34:37
winform程序 我用一个timer控件 每200MS执行一次 用来获取数据 当获取到数据的时候 会弹出新窗体 但是弹出的新窗体会卡死
在不停用该timer控件的情况下 怎么优化其性能 (试过用线程 但好像也有这样的问题 不知道是不是我线程没写好)
...全文
1631 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
david_88888 2013-09-11
  • 打赏
  • 举报
回复
引用 12 楼 cncnm 的回复:
引用 11 楼 u011337895 的回复:
你的程序每两百毫秒会弹出一个窗体,也就是说不停的弹出,你说会卡死吗、?搞不懂你为什么这样做
接受到信息的时候(有判断)才会弹出 用的单例模式 只弹出一个窗体
那每次都有数据呢?是不是设计的有问题
卧_槽 2013-09-11
  • 打赏
  • 举报
回复
在弹出新窗体之前把timer stop()一下,处理完了重新start()
大新 2013-09-11
  • 打赏
  • 举报
回复
那你就在定时器里加Appliction.DoEvents()
周美文 2013-09-11
  • 打赏
  • 举报
回复
timer弹出窗体肯定是没问题的,你用个循环messagebox.show()是可以无限弹出的。 只是想不通你为什么要这么设计呢?
hehehndy 2013-09-10
  • 打赏
  • 举报
回复
引用 15 楼 hyxwill 的回复:
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); } }
这样写的话 弹出的窗体会被卡死...
大新 2013-09-10
  • 打赏
  • 举报
回复
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); } }
菜菜菜菜鸟 2013-09-10
  • 打赏
  • 举报
回复
引用 13 楼 hyxwill 的回复:
如果没有应用到其他主线程东西,你就在线程直接new Form().Show()也是可以的
就timer控件在执行 帮忙看看代码 在8楼
大新 2013-09-10
  • 打赏
  • 举报
回复
如果没有应用到其他主线程东西,你就在线程直接new Form().Show()也是可以的
菜菜菜菜鸟 2013-09-10
  • 打赏
  • 举报
回复
引用 11 楼 u011337895 的回复:
你的程序每两百毫秒会弹出一个窗体,也就是说不停的弹出,你说会卡死吗、?搞不懂你为什么这样做
接受到信息的时候(有判断)才会弹出 用的单例模式 只弹出一个窗体
编程达人G 2013-09-10
  • 打赏
  • 举报
回复
你的程序每两百毫秒会弹出一个窗体,也就是说不停的弹出,你说会卡死吗、?搞不懂你为什么这样做
菜菜菜菜鸟 2013-09-10
  • 打赏
  • 举报
回复
代码在8楼 不能沉啊
菜菜菜菜鸟 2013-09-10
  • 打赏
  • 举报
回复
求帮忙啊 大神们...
hehehndy 2013-09-10
  • 打赏
  • 举报
回复
引用 7 楼 hdhai9451 的回复:
文字表达不好的,直接帖出代码,这是最好解决问题的办法
 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;
        }
Andy__Huang 2013-09-10
  • 打赏
  • 举报
回复
文字表达不好的,直接帖出代码,这是最好解决问题的办法
blwuer 2013-09-10
  • 打赏
  • 举报
回复
引用 5 楼 blwuer 的回复:

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

private void ShowMsg(string msg)
        {
            if (this.InvokeRequired)
            {
                Action<string> at = ShowMsg;
                this.Invoke(at, msg);
            }
            else
            {
                //显示窗口代码
                new FormMsg(msg).Show();
            }
        }
blwuer 2013-09-10
  • 打赏
  • 举报
回复

private void ShowMsg(string msg)
        {
            if (this.InvokeRequired)
            {
                Action<string, bool> at = ShowMsg;
                this.Invoke(at, msg);
            }
            else
            {
                //显示窗口代码
                new FormMsg(msg).Show();
            }
        }
blwuer 2013-09-10
  • 打赏
  • 举报
回复
你timer线程中创建的窗体肯定没时间响应你的动作,因为线程都一直在工作。正确做法是在主窗口中提供一个方法用于调用委托显示新窗体。
华为黑名单 2013-09-10
  • 打赏
  • 举报
回复
http://www.cnblogs.com/Zeech-Lee/archive/2011/10/14/2212376.html
threenewbee 2013-09-10
  • 打赏
  • 举报
回复
说得太笼统!天知道你代码怎么写的。
华为黑名单 2013-09-10
  • 打赏
  • 举报
回复
BeginInvoke 委托开启新窗体!

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧