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!";
}));
});