public partial class ThreadTest : Form
{
Thread Thd1;
Thread Thd2;
private delegate void DelegateLabelController(Label lbl, int i);
public ThreadTest()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
Application.DoEvents();
ThreadStart Ts1 = new ThreadStart(Process1); //執行緒起動要呼叫的function
ThreadStart Ts2 = new ThreadStart(Process2);
Thd1 = new Thread(Ts1); //ThreadStart
Thd2 = new Thread(Ts2);
Thd1.Start(); //起動執行緒
Thd2.Start();
}
//由delegate 建立的實體
private void Process1()
{
for (int i = 0; i < 1000; i++)
{
//.....可能實際存取資料庫或其他程式運算
DelegateLabelController delLabelController = new DelegateLabelController(ProcessAction1); //由delegate 建立的實體, 去呼叫實際執行動作的 function
delLabelController(label1, i); //由delegate 建立的實體 帶入實際參數,實際上執行的是 Process1Action1(label1, i)
Thread.Sleep(1); //*讓Thrad睡一下,才能讓Thread有空跟UI做溝通
}
}
private void Process2()
{
for (int i = 0; i < 1000; i++)
{
//.....可能實際存取資料庫或其他程式運算
DelegateLabelController delLabelController = new DelegateLabelController(ProcessAction2); //由delegate 建立的實體, 去呼叫實際執行動作的 function
delLabelController(label2, i); //由delegate 建立的實體 帶入實際參數,實際上執行的是 Process1Action2(label1, i)
Thread.Sleep(1); //*讓Thrad睡一下,才能讓Thread有空跟UI做溝通
}
}
public void ProcessAction1(Label lbl, int i)
{
if(lbl == null || i == null)
{
return;
}
if (lbl.InvokeRequired)
{
lbl.Text = i.ToString();
if(i == 999)
{
lbl.Text = "Complete";
button1.Enabled = true;
Application.DoEvents();
}
}
}
public void ProcessAction2(Label lbl, int i)
{
if (lbl == null || i == null)
{
return;
}
if (lbl.InvokeRequired)
{
lbl.Text = i.ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
Thd1.Abort(); //Stop Thread.
}
//用呼叫Class的方式
private void button3_Click(object sender, EventArgs e)
{
button3.Enabled = false;
Application.DoEvents();
try //帶參數 要用 ParameterizedThreadStart
{
ParameterizedThreadStart Ts1ToClass = new ParameterizedThreadStart(new Class1().Process1); //執行緒起動要呼叫的function
ParameterizedThreadStart Ts2ToClass = new ParameterizedThreadStart(new Class1().Process2);
Thread Thread1ToClass = new Thread(Ts1ToClass); //ThreadStart
Thread Thread2ToClass = new Thread(Ts2ToClass);
Thread1ToClass.Start("PROCESS1"); //起動執行緒 並帶入參數
Thread2ToClass.Start("PROCESS2");
MessageBox.Show("呼叫Class 執行完成");
System.Threading.Thread.Sleep(1000);
button3.Enabled = true;
Application.DoEvents();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
----------------------------------------------------------------------------
public class Class1
{
//只能執行程式
public void Process1(object processName)
{
//MessageBox.Show(processName.ToString());
for (int i = 0; i < 10; i++)
{
//.....可能實際存取資料庫或其他程式運算
Thread.Sleep(1); //*讓Thrad睡一下,才能讓Thread有空跟UI做溝通
}
;
}
public void Process2(object processName)
{
//MessageBox.Show(processName.ToString());
for (int i = 0; i < 10; i++)
{
//.....可能實際存取資料庫或其他程式運算
Thread.Sleep(1); //*讓Thrad睡一下,才能讓Thread有空跟UI做溝通
}
}
}
{
Thread Thd1;
Thread Thd2;
private delegate void DelegateLabelController(Label lbl, int i);
public ThreadTest()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
Application.DoEvents();
ThreadStart Ts1 = new ThreadStart(Process1); //執行緒起動要呼叫的function
ThreadStart Ts2 = new ThreadStart(Process2);
Thd1 = new Thread(Ts1); //ThreadStart
Thd2 = new Thread(Ts2);
Thd1.Start(); //起動執行緒
Thd2.Start();
}
//由delegate 建立的實體
private void Process1()
{
for (int i = 0; i < 1000; i++)
{
//.....可能實際存取資料庫或其他程式運算
DelegateLabelController delLabelController = new DelegateLabelController(ProcessAction1); //由delegate 建立的實體, 去呼叫實際執行動作的 function
delLabelController(label1, i); //由delegate 建立的實體 帶入實際參數,實際上執行的是 Process1Action1(label1, i)
Thread.Sleep(1); //*讓Thrad睡一下,才能讓Thread有空跟UI做溝通
}
}
private void Process2()
{
for (int i = 0; i < 1000; i++)
{
//.....可能實際存取資料庫或其他程式運算
DelegateLabelController delLabelController = new DelegateLabelController(ProcessAction2); //由delegate 建立的實體, 去呼叫實際執行動作的 function
delLabelController(label2, i); //由delegate 建立的實體 帶入實際參數,實際上執行的是 Process1Action2(label1, i)
Thread.Sleep(1); //*讓Thrad睡一下,才能讓Thread有空跟UI做溝通
}
}
public void ProcessAction1(Label lbl, int i)
{
if(lbl == null || i == null)
{
return;
}
if (lbl.InvokeRequired)
{
lbl.Text = i.ToString();
if(i == 999)
{
lbl.Text = "Complete";
button1.Enabled = true;
Application.DoEvents();
}
}
}
public void ProcessAction2(Label lbl, int i)
{
if (lbl == null || i == null)
{
return;
}
if (lbl.InvokeRequired)
{
lbl.Text = i.ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
Thd1.Abort(); //Stop Thread.
}
//用呼叫Class的方式
private void button3_Click(object sender, EventArgs e)
{
button3.Enabled = false;
Application.DoEvents();
try //帶參數 要用 ParameterizedThreadStart
{
ParameterizedThreadStart Ts1ToClass = new ParameterizedThreadStart(new Class1().Process1); //執行緒起動要呼叫的function
ParameterizedThreadStart Ts2ToClass = new ParameterizedThreadStart(new Class1().Process2);
Thread Thread1ToClass = new Thread(Ts1ToClass); //ThreadStart
Thread Thread2ToClass = new Thread(Ts2ToClass);
Thread1ToClass.Start("PROCESS1"); //起動執行緒 並帶入參數
Thread2ToClass.Start("PROCESS2");
MessageBox.Show("呼叫Class 執行完成");
System.Threading.Thread.Sleep(1000);
button3.Enabled = true;
Application.DoEvents();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
----------------------------------------------------------------------------
public class Class1
{
//只能執行程式
public void Process1(object processName)
{
//MessageBox.Show(processName.ToString());
for (int i = 0; i < 10; i++)
{
//.....可能實際存取資料庫或其他程式運算
Thread.Sleep(1); //*讓Thrad睡一下,才能讓Thread有空跟UI做溝通
}
;
}
public void Process2(object processName)
{
//MessageBox.Show(processName.ToString());
for (int i = 0; i < 10; i++)
{
//.....可能實際存取資料庫或其他程式運算
Thread.Sleep(1); //*讓Thrad睡一下,才能讓Thread有空跟UI做溝通
}
}
}