8.委托的跨線程操控
在此,我以人們常說的跨線程操控控件為例。首先,如果你已經掌握了C#多線程的基本用法,不妨自己試著實現一下這段程序。在未掌握委托之前,我曾經這樣寫過:
using System.Threading;
namespace MulTrdDelegate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//初始化子線程對象
private Thread demoThread = null;
private void button1_Click(object sender, EventArgs e)
{
demoThread = new Thread(new ThreadStart(ThreadProcUnsafe));
demoThread.Start();
}
public void ThreadProcUnsafe()
{
textBox1.Text = "這個控件的內容是由子線程實現的";
}
}
}