WPF多线程(后台线程_和异步执行)

BackgroundWorker BarckgroundworkerBtn;
private void button1_Click(object sender, RoutedEventArgs e)
{
BarckgroundworkerBtn = new BackgroundWorker();
BarckgroundworkerBtn.DoWork += new DoWorkEventHandler(BarckgroundworkerBtn_DoWork);
BarckgroundworkerBtn.RunWorkerAsync();

}
private void BarckgroundworkerBtn_DoWork(object sender, DoWorkEventArgs e)
{
Start();
}

 

、、、异步执行

//异步开始执行
Func<string> func = new Func<string>(() =>
{
this.Dispatcher.Invoke(new Action(() =>
{
for (int d = 0; d < 10; d++)
{
d++;
Thread.Sleep(1000);
progressBar1.Value = d;
}
}));
return "";
});
//异步完成时调用的方法
AsyncCallback callback = new AsyncCallback((i) =>
{
this.Dispatcher.Invoke(new Action(() =>
{
label1.Content = "异步执行完成1!";
}));
});

posted @ 2012-03-09 15:37  @张凯@  阅读(4690)  评论(0编辑  收藏  举报