有时需要新增线程执行可能有延迟的语句,如从远程数据库中提取数据实时验效时:
System.Threading.Thread th =new System.Threading.Thread
(
()=>BackstageMethod();
);
th.Start();
(
()=>BackstageMethod();
);
th.Start();
但是请注意根据WPF实现的安全模型,只有一个用户的对象自己创建的线程才能访问该对象,这就是说,后台线程不能直接修改界面对象。在WPF中,则需要使用控件上的System.Threading.Dispatcher对象来执行封送操作。Dispatcher的Invoke方法可发送一个请求,如:
1 using System.Threading;
2 using System.Windows.Threading;
3
4
5 void textBox1_LostKeyboardFocus(object sender,,KeyboardFocusChangedEventArgs e)
6 {
7 Thread th =new Thread
8 (
9 () =>bw_DoWork("第二线程传送的值")
10 );
11 //执行第线线程;
12 th.Start();
13 }
14 void bw_DoWork(string t)
15 {
Thread.Sleep(1000);
16 //将要执行的方法放入一个委托实例中
17 Action action =new Action(
18 () => textBox2.Text=t
19 );
20 //在应用程序空闲时处理操作
21 Dispatcher.Invoke(action, DispatcherPriority.ApplicationIdle);
22 }
2 using System.Windows.Threading;
3
4
5 void textBox1_LostKeyboardFocus(object sender,,KeyboardFocusChangedEventArgs e)
6 {
7 Thread th =new Thread
8 (
9 () =>bw_DoWork("第二线程传送的值")
10 );
11 //执行第线线程;
12 th.Start();
13 }
14 void bw_DoWork(string t)
15 {
Thread.Sleep(1000);
16 //将要执行的方法放入一个委托实例中
17 Action action =new Action(
18 () => textBox2.Text=t
19 );
20 //在应用程序空闲时处理操作
21 Dispatcher.Invoke(action, DispatcherPriority.ApplicationIdle);
22 }
另外,可以使用Tast类来操作:可以将上面变成Tast tast = New Tesk(delegate{bw_DoWork("this is Task")});
如果是一连串有顺序的Task可以:
Task first = Task.Factory.StartNew(() => generateGraphData(data, 0, pixelWidth /8));
Task second = Task.Factory.StartNew(() => generateGraphData(data, pixelWidth /8, pixelWidth /4));
Task third = Task.Factory.StartNew(() => generateGraphData(data, pixelWidth /4, pixelWidth *3/8));
Task fourth = Task.Factory.StartNew(() => generateGraphData(data, pixelWidth *3/8, pixelWidth /2));
Task.WaitAll(first, second, third, fourth);
Task second = Task.Factory.StartNew(() => generateGraphData(data, pixelWidth /8, pixelWidth /4));
Task third = Task.Factory.StartNew(() => generateGraphData(data, pixelWidth /4, pixelWidth *3/8));
Task fourth = Task.Factory.StartNew(() => generateGraphData(data, pixelWidth *3/8, pixelWidth /2));
Task.WaitAll(first, second, third, fourth);
最后再介绍一个我最近用得较多的类:BackgroundWorker, 它允许你在单独的线程上操作,并将操结果获取后送出来,下面是一个例子:
public partial class MainPage : UserControl { private BackgroundWorker worker = new BackgroundWorker(); public MainPage() { InitializeComponent(); radBusyIndicator.IsBusy = true; worker.DoWork += InitialGvMain;//在单独的线程上操作,其中的e.Result获取操作结果 worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);//在操作完成时用e.Result接收上面的操作结果 worker.RunWorkerAsync();//异步操作开始 } void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { //Action action = new Action( // () => // { gvMain.ItemsSource = e.Result;//接收InitialGvMain中的view radBusyIndicator.IsBusy = false; // } // ); //Dispatcher.BeginInvoke(action); } private void InitialGvMain(object sender, DoWorkEventArgs e) { Guests RG = new Guests(); QueryableCollectionView view = new QueryableCollectionView(RG.GetRandomGuest(55)); view.PageSize = 10; e.Result = view;//结果类型是任意的 } }