C#windows项目解决线程卡死问题

while (true) { if (!isExist()) break; Thread.Sleep(Convert.ToInt32(textDelay.Text)*1000); if (check()) break; for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++) { string str = getTableName(checkedListBox1.CheckedItems[i].ToString()); string s1 = String.Format("update a set a.EIP =2 , a.UPLOAD_TIME = (select convert(varchar,GETDATE (),20)) from {0} a ", str); string s2 = String.Format("where a.ID in (select top {0} id from {1} b where b.EIP =3 order by id ); ", Convert.ToInt32(txtNum.Text), str); string s = s1 + s2; richTextBox1.Text = s; SqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, s); } count++; label5.Text = count.ToString(); Application.DoEvents(); }

例如这样的循环,在界面上运行会造成循环卡死,我们的需求是界面不卡、正常运行,并且label上显示循环的次数。

为什么会卡死?这个问题我也不太知道,我猜测应该是C#程序运行的时候先处理代码,代码运行完了,再渲染至UI上。

下面来说说解决方案:

1、在循环内写Application.DoEvents();

效果:label能正常显示,界面仍有一些卡顿

原理:

Application.DoEvents 方法

当运行 Windows 窗体时,它将创建新窗体,然后该窗体等待处理事件。该窗体在每次处理事件时,均将处理与该事件关联的所有代码。所有其他事件在队列中等待。在代码处理事件时,应用程序并不响应。例如,当将另一窗口拖到该窗口前面时,该窗口不重新绘制。

如果在代码中调用 DoEvents,则应用程序可以处理其他事件。例如,如果向 ListBox 添加数据的窗体,并将 DoEvents 添加到代码中,那么当将另一窗口拖到该窗体上时,该窗体将重新绘制。如果从代码中移除 DoEvents,那么在按钮的单击事件处理程序执行结束以前,改窗体不会重新绘制。

通常,在循环中使用该方法来处理消息。

2、用多线程解决

处理思路:运行长时间的代码单独声明一个线程。耗时的操作不能放在主线程中,卡死就是因为主线程被阻塞了。 要将处理函数放到单独线程中去, 有一点需要注意,如果线程中有与UI交互的处理, 那就得用委托或匿名方法。

原理:程序运行时,会从线程池里随意挑选一个线程执行,这个线程不能满足运行时间长的程序,主线程负责ui的渲染,主线程的任务特别耗时,就会导致无法处理ui任务,ui就会卡死。

public void Run() { int count = 0; while (true) { if (!isExist()) break; Thread.Sleep(Convert.ToInt32(textDelay.Text) * 1000); if (check()) break; for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++) { string str = getTableName(checkedListBox1.CheckedItems[i].ToString()); string s1 = String.Format("update a set a.EIP =2 , a.UPLOAD_TIME = (select convert(varchar,GETDATE (),20)) from {0} a ", str); string s2 = String.Format("where a.ID in (select top {0} id from {1} b where b.EIP =3 order by id ); ", Convert.ToInt32(txtNum.Text), str); string s = s1 + s2; richTextBox1.Text = s; SqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, s); } count++; label5.Text = count.ToString(); Application.DoEvents(); } }

private void btnStart_Click(object sender, EventArgs e) { try { Thread th = new Thread(Run); th.IsBackground = true; th.Start(); //MessageBox.Show("count"); } catch(Exception ex) { MessageBox.Show("失败:" + ex.Message); } }

再使用委托

public string connectionString = ""; public delegate void ShowDelegate(string s,int count); Thread th;

public void Run() { int count = 0; while (true) { string s = ""; if (this.label5.InvokeRequired && this.richTextBox1.InvokeRequired) { ShowDelegate showDelegate = new ShowDelegate(show); //showDelegate(s, count); this.Invoke(showDelegate, new object[] { s, count }); } else show(s, count); if (!isExist()) break; Thread.Sleep(Convert.ToInt32(textDelay.Text) ); if (check()) break; for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++) { string str = getTableName(checkedListBox1.CheckedItems[i].ToString()); string s1 = String.Format("update a set a.EIP =2 , a.UPLOAD_TIME = (select convert(varchar,GETDATE (),20)) from {0} a ", str); string s2 = String.Format("where a.ID in (select top {0} id from {1} b where b.EIP =3 order by id ); ", Convert.ToInt32(txtNum.Text), str); s = s1 + s2; SqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, s); } count++; Application.DoEvents(); } } public void show(string s,int count) { richTextBox1.Text = s; label5.Text = count.ToString(); if (count != 0) lbZT.Text = "正在运行"; }

OK,查阅百度,这样问题就可以解决了。

接下来的问题是invoke是什么意思?

参考博客C#中Invoke的用法_lk989898的博客-CSDN博客_c# invoke

你可能感兴趣的:(c#,windows,ui)