C# 多线程,解决处理大数据时窗体(不能拖动等)假死现象
using System.Threading;//引入命名空間
public partial class Form3 : Form
??? {
??????? public Form3()
??????? {
??????????? InitializeComponent();
??????? }
??????? // 第一種方法:
??????? delegate void MyDel(int i);//此處定義委托應與方法HH的原型一致
??????? MyDel mydel = null;
??????? private void button1_Click(object sender, EventArgs e)
??????? {
??????????? mydel = new MyDel(HH);
??????????? Thread h = new Thread(new ThreadStart(World));
??????????? h.IsBackground = true;
??????????? h.Start();
??????? }
??????? void HH(int j)
??????? {
??????????? #region 錯誤方法
??????????? //for (int i = 0; i <= j; i++)
??????????? //{
??????????? //??? Thread.Sleep(100);
??????????? //??? this.progressBar1.Value = i;
??????????? //??? if (i == 100)
??????????? //??? {
??????????? //??????? if(DialogResult.OK== MessageBox.Show("成功","測試",MessageBoxButtons.OKCancel,MessageBoxIcon.Question))
??????????? //??????? {
??????????? //??????????? this.Close();
??????????? //??????? }
??????????? //??? }
??????????? //}
??????????? #endregion
??????????? this.progressBar1.Value = j;
??????????? this.label1.Text = "當前已完成" + j.ToString() + "%,請稍等……";
??????????? if (j == 100)
??????????? {
??????????????? this.Invoke(new MethodInvoker(delegate() { this.label1.Text = "完成"; }));
??????????????? if (DialogResult.OK == MessageBox.Show("成功2", "測試2", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
??????????????? {
??????????????????? this.Close();
??????????????? }
??????????? }
??????? }
??????? void World()
??????? {
??????????? mydel = new MyDel(HH);
??????????? for (int i = 0; i <= 100; i++)
??????????? {
??????????????? Thread.Sleep(100);
??????????????? BeginInvoke(mydel, new object[] { i });
??????????? }
??????? }
??????? //第二種方法
??????? private void button2_Click(object sender, EventArgs e)
??????? {
??????????? Thread t = new Thread(new ThreadStart(MessageShow));
??????????? t.IsBackground = true;
??????????? t.Start();
??????? }
??????? void MessageShow()
??????? {
??????????? for (int i = 0; i <= 100; i++)
??????????? {
??????????????? Thread.Sleep(100);
??????????????? this.Invoke(new MethodInvoker(delegate() { this.label3.Text = "當前已完成" + i + "%,請稍等……"; }));
??????????????? if (i == 100)
??????????????? {
??????????????????? this.Invoke(new MethodInvoker(delegate()
??????????????????? {
??????????????????????? this.label3.Text = "完成";
??????????????????????? if (DialogResult.OK == MessageBox.Show("完成1", "完成1", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
??????????????????????? {
??????????????????????????? this.Close();
??????????????????????? }
??????????????????????? else
??????????????????????? {
??????????????????????? }
??????????????????? }));
??????????????? }
??????????????? this.Invoke(new MethodInvoker(delegate() { this.progressBar2.Value = i; }));
??????????? }
??????? }
??????
??????? //第三種方法
??????? private void button3_Click(object sender, EventArgs e)
??????? {
??????????? Thread th = new Thread(new ParameterizedThreadStart(GetPath));
??????????? th.IsBackground = true;
??????????? th.Start("100");
??????? }
??????? void GetPath(object str)
??????? {
??????????? int j = int.Parse(str.ToString());
??????????? for (int i = 0; i <= j; i++)
??????????? {
??????????????? Thread.Sleep(100);
??????????????? this.Invoke(new MethodInvoker(delegate() { this.progressBar3.Value = i; }));
??????????????? this.Invoke(new MethodInvoker(delegate() { this.label2.Text = "當前已完成" + i.ToString() + "%,請稍等……"; }));
??????????????? if (i == 100)
??????????????? {
??????????????????? this.Invoke(new MethodInvoker(delegate() { this.label2.Text = "完成" ; }));
??????????????????? if (DialogResult.OK == MessageBox.Show("成功3", "測試3", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
??????????????????? {
??????????????????????? this.Close();
??????????????????? }
??????????????? }
??????????? }
??????? }//此方法必須是Object類型的參數
??? }
}
?
?
另外說明:
?
在多線程程序中,新創建的線程不能訪問UI線程創建的窗口控件,如果需要訪問窗口中的控件,可以在窗口構造函數中將CheckForIllegalCrossThreadCalls設置為 false
public Form1() { ???
InitializeComponent(); ??
? CheckForIllegalCrossThreadCalls = false;
}
?
也可以針對某一控件進行設置,例如:
??? TextBox.CheckForIllegalCrossThreadCalls = false;
轉載于:https://www.cnblogs.com/cbwbin/archive/2012/07/20/2601205.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的C# 多线程,解决处理大数据时窗体(不能拖动等)假死现象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cross product
- 下一篇: Delphi实现类似Android锁屏的