日期:2014-05-18 浏览次数:20753 次
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); } }