我想用BackgroundWorker实现暂停恢复和取消功能。我有一个用户控件和所有与用户控件编写的backgroundworker相关的代码。我只是在用户点击按钮的时候添加用户控制流布局控制。我没有受到阻挠 暂停&使用 ManualResetEvent 恢复功能,但无论如何我无法做到。所以我在这里粘贴我的用户控件类相关代码和我的表单代码。
public partial class ucBackgroundWorker : UserControl
{
System.ComponentModel.BackgroundWorker bgWorker = null;
public event Action<string, EventArgs> Done;
public event Action<string, EventArgs> Cancel;
private static bool m_continue = true;
private ManualResetEvent _resetEvent = new ManualResetEvent(false);
//Semaphore sWaiter = new Semaphore(0, 1);
public ucBackgroundWorker()
{
InitializeComponent();
bgWorker = new System.ComponentModel.BackgroundWorker();
bgWorker.WorkerSupportsCancellation = true;
bgWorker.WorkerReportsProgress = true;
bgWorker.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
bgWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
}
public void Run(int counter)
{
if (!bgWorker.IsBusy)
{
bgWorker.RunWorkerAsync(counter);
}
_resetEvent.Set();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int input = int.Parse(e.Argument.ToString());
this.BeginInvoke((MethodInvoker)delegate
{
lblStatus.Text = "Running";
});
for (int i = 1; i <= input; i++)
{
_resetEvent.WaitOne();
Thread.Sleep(500);
(sender as System.ComponentModel.BackgroundWorker).ReportProgress(i * 10);
if ((sender as System.ComponentModel.BackgroundWorker).CancellationPending)
{
this.BeginInvoke((MethodInvoker)delegate
{
lblStatus.Text = "Cancel";
});
e.Cancel = true;
return;
}
}
Thread.Sleep(1000);
}
// This event handler deals with the results of the
// background operation.
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// First, handle the case where an exception was thrown.
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{
if (Cancel != null)
Cancel(this.Name, EventArgs.Empty);
}
else
{
this.BeginInvoke((MethodInvoker)delegate
{
lblStatus.Text = "Done";
});
if (Done != null)
Done(this.Name, EventArgs.Empty);
}
_resetEvent.Reset();
}
// This event handler updates the progress bar.
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pBar.Refresh();
pBar.Value = e.ProgressPercentage;
}
private void btnCancel_Click(object sender, EventArgs e)
{
if (bgWorker.IsBusy)
{
bgWorker.CancelAsync();
}
}
private void btnPause_Click(object sender, EventArgs e)
{
if (bgWorker.IsBusy)
{
if (btnPause.Text.ToUpper() == "PAUSE")
{
btnPause.Text = "Resume";
m_continue = false;
_resetEvent.Reset();
}
else if (btnPause.Text.ToUpper() == "RESUME")
{
btnPause.Text = "Pause";
m_continue = true;
_resetEvent.Set();
}
}
}
}
public partial class Form3 : Form
{
ucBackgroundWorker ucBgWorker = null;
public Form3()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
ucBgWorker = new ucBackgroundWorker();
ucBgWorker.Done += new Action<string, EventArgs>(Worker_Done);
ucBgWorker.Cancel += new Action<string, EventArgs>(Worker_Cancel);
flowLayoutPanel1.Controls.Add(ucBgWorker);
ucBgWorker.Run(10);
}
void Worker_Done(string arg, EventArgs evtarg)
{
label1.Text =arg + " Done One";
//System.Threading.Thread.Sleep(1000);
}
void Worker_Cancel(string arg, EventArgs evtarg)
{
label1.Text = arg + " Cancel Click";
//System.Threading.Thread.Sleep(1000);
}
}
这里我附上mu UI的图片看起来如何。
答案 0 :(得分:2)
为您的班级添加属性,该属性将保留bool - Sleep。订阅该更改,如果确实将thread.sleep设置为某个大数字,则订阅该数字变为0,如果bool仍为True,则将其设置为大数字。这就是我现在想到的更好的方法。