BackgroundWorker应用的若干疑问?
BackgroundWorker应用问题
第一个问题:
如果我事先不知道要定义多少个BackgroundWorker
必须要通过读取config.xml来定义BackgroundWorker的数量
比如config.xml内容为下
task1
task2
task3
通过XML读取这个任务,所以要定义三个BackgroundWorker要如何定义?
第二个问题:
我要执行这三个任务并行处理,又要怎么办,而不是按顺序来执行之?
也就是task1,task2,task3三个一起执行,不管有没有执行结束?
第三个问题:
如何重载Background使得在BackgroundWoker的OnDoWorker()事件中新加入一个参数?
第四个问题:
还没想好...想好了再continue...
Thanks!!!
private BackgroundWorker worker;worker = new BackgroundWorker();worker.WorkerReportsProgress = true;worker.WorkerSupportsCancellation = true;worker.DoWork += new DoWorkEventHandler(worker_DoWork);worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
public void Start() { string[] config = { "task1", "task2", "task3" }; foreach (string task in config) //<--- 1 { BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += DoWork; worker.RunWorkerAsync(task); //<--- 2,3 } } void DoWork(object sender, DoWorkEventArgs e) { string taskDescription = e.Argument as string; //<--- 3 while (true) { Console.WriteLine(taskDescription); System.Threading.Thread.Sleep(500); } }