private void AutoWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; WorkArgs args = e.Argument as WorkArgs; e.Result = args.Type; switch (args.Type) { case "开始": DoWork(worker, args.Item); break; } if (worker.CancellationPending) { e.Cancel = true; } }
private void timer1_Tick(object sender, EventArgs e) { lbltime.Text = DateTime.Now.ToString(); if (!AutoWorker.IsBusy) { count++; if (count > SleepTime) { count = 0; btnStart.Enabled = false; WorkArgs args = new WorkArgs() { Type = "开始", Item = null }; AutoWorker.RunWorkerAsync(args); } } else { //MessageBox.Show("后台线程正在工作,请等待上一个工作完成"); } }