问题
i want to implement pause resume and cancel functionality with BackgroundWorker. i have a one user control and all backgroundworker related code written in user control. i just add user control on flow layout control as many as time user click on buttons. i am fail to impelement Pause & Resume functionality with ManualResetEvent but anyway i am not being able to do it. so here i am pasting my user control class related code and my form code.
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);
}
}
here i am attaching pic of mu UI how it looks.
回答1:
Add property to your class which will hold bool - Sleep . Subscribe to that change and if it's true set thread.sleep to some big number, then subscribe to that number becoming 0 and if the bool is still True set it to big number again. That's what comes to my mind now there are better ways for sure.
来源:https://stackoverflow.com/questions/15722232/how-to-implement-pause-resume-functionality-with-backgroundworker-c-sharp