Pio*_*otr 1 c# backgroundworker
我今天做了一个小测试,以了解有关BackgroundWorker的更多信息.
在我看来,它不适用于asychronuos模式.首先它做了Do1和下一个Do2.Do2更短,Do1需要更多时间,但是程序等待Do1完成并且下一次启动Do2.我对吗?谢谢!
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Transactions;
namespace ConsoleApplication16
{
public interface I
{
void UstawWiek(string w);
void PokazWiek();
}
class rrr
{
public delegate void MojDelegat();
public static void Do1(object sender, DoWorkEventArgs e)
{
System.Threading.Thread.Sleep(4000);
Console.WriteLine("Do1");
}
public static void Do2(object sender, DoWorkEventArgs e)
{
System.Threading.Thread.Sleep(1000);
Console.WriteLine("Do2");
}
static void Main(string[] args)
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(Do1);
bw.DoWork += new DoWorkEventHandler(Do2);
bw.RunWorkerAsync();
int i =0;
while ( bw.IsBusy)
{
Console.WriteLine("Waiting {0}",i);
System.Threading.Thread.Sleep(100);
i++;
}
Console.WriteLine("Done!");
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
您添加了两个事件处理程序BackgroundWorker.
与所有其他事件一样,DoWork事件将按顺序同步运行其所有处理程序.
要异步运行两个单独的东西,你需要两个BackgroundWorker.
但是,你应该使用Task.Run(); 它更简单,更易于组合.
| 归档时间: |
|
| 查看次数: |
179 次 |
| 最近记录: |