Why my program executed after I cancel BackgroundWorker? - c#

I am practicing to use BackgroundWorker to do some time-consuming jobs.
My goal is showing all the file paths on UI.
I press the RUN button and press the CANCEL button after executing it for 5 seconds. It works fine.
Then I repeat the above steps, I notice that "Check_File" is executed for the third time.
I think that it shall be only two times.
The UI shows like below:
Searching for Files → Many FileName Appear → Canceled
Searching for Files → Many FileName Appear → Searching for Files → Canceled
The following is my code:
private void Run_Click(object sender, RoutedEventArgs e)
{
worker.WorkerSupportsCancellation = true;
worker.WorkerReportsProgress = true;
worker.DoWork += Worker_DoWork;
worker.ProgressChanged += Worker_ProgressChanged;
worker.RunWorkerAsync(PText.Text);
worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
}
private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled == true)
{
FLabel.Content = "Canceled";
}
else
{
FLabel.Content = "Finish";
}
worker.Dispose();
}
private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
FLabel.Content = e.UserState.ToString();
}
private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
Check_File("My_Folder_Path", (String)e.Argument, e);
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
worker.CancelAsync();
}
public void Check_File(string Path, string FindStr, DoWorkEventArgs e)
{
string v_alert;
i += 1;
try
{
if (Path.LastIndexOf('\\') != Path.Length)
{
Path = string.Concat(Path, "\\");
}
if (Directory.Exists(Path))
{
worker.ReportProgress(0, string.Concat("Searching for Files...", i.ToString()));
string[] Files = Directory.GetFiles(Path);
if (Files.Length != 0)
{
foreach (string f in Files)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
worker.ReportProgress(Array.IndexOf(Files, f) / Files.Length * 100, f);
}
}
}
else
{
v_alert = string.Concat("No Path:", Path);
MessageBox.Show(v_alert);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Everytime you press the button you add an event to the worker.
private void Run_Click(object sender, RoutedEventArgs e)
{
worker.WorkerSupportsCancellation = true;
worker.WorkerReportsProgress = true;
worker.DoWork += Worker_DoWork; //<----Happens here
worker.ProgressChanged += Worker_ProgressChanged;
worker.RunWorkerAsync(PText.Text);
worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
}
So if you press it once. The worker has one event attached. If you press it a second time, the worker get attached another event. Hes now has two events. Hes runs again (this time with 2 events) and there is a total of three runs.
You should set up the worker somewhere else.
public Form1()
{
InitializeComponent();
//Init the worker here...
worker.WorkerSupportsCancellation = true;
worker.WorkerReportsProgress = true;
worker.DoWork += Worker_DoWork;
worker.ProgressChanged += Worker_ProgressChanged;
worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
}
private void Run_Click(object sender, RoutedEventArgs e)
{
//only run the worker here
worker.RunWorkerAsync(PText.Text);
}
As Shocky mentioned in the comments, you could also remove the existing event before you attach a new one.

Related

Can we call method in DoWork event?

I am exporting a Data Table to excel file using background worker. And I wanted to show the export progress using progress bar. Do we have to write the exporting code in Do Work event or can we call a method, which is present in other class.
In my code I tried calling different method. But its not working.
Below is the sample code.
public MainWindow()
{
InitializeComponent();
property = new Property();
this.DataContext = property;
worker.WorkerReportsProgress = true;
worker.DoWork += worker_DoWork;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
System.Windows.Forms.MessageBox.Show(e.Error.Message);
}
else
{
System.Windows.Forms.MessageBox.Show("Exported Successfully");
}
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pbStatus.Value = e.ProgressPercentage;
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
Export export = new Export();
export.GenerateExcelFile();
}
You need to call worker.ReportProgress from worker_DoWork with a valid progress value
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
worker.ReportProgress(0);
// Some job
worker.ReportProgress(10);
// ...
// Finish
worker.ReportProgress(100);
}
I'm not sure how you are generating report. Further I'm supposing your pbStatus has Minimum="0" and Maximum="100". You can after exporting each row report progress something like that.
worker.ReportProgress(currentRow * 100.0 / totalRows);
You also set your progress bar intermediate if you are not sure how to calculate that by setting progress.IsIndeterminate to true
pbStatus.IsIndeterminate = true;

Check internet connection is available or not

I am trying to detect the internet connection, if the internet connection is available and it is connected, it will continue, otherwise it will throw message box says that the connection is not available.
What I am encounter is whether the internet connection is connected or not connected, the code will continue.
Here is the code:
** The program will continue to worker_ProgressChanged, even though there is no internet connection available **
public CheckUpdates()
{
InitializeComponent();
bool checkConnection = CheckConnection.IsConnectedToInternet();
progressBar1.Style = ProgressBarStyle.Marquee;
if (checkConnection == true)
{
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += new DoWorkEventHandler(worker_DoWork);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
}
else
{
System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(#"C:\Windows\Media\Windows Notify.wav");
_sound.Play();
DialogResult _dialogResult = MessageBox.Show("No connection available, please check your internet connection!", "No connection");
if (_dialogResult == DialogResult.OK)
{
this.Hide();
this.Close();
}
}
}
private void CheckUpdates_Load(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
if (e.ProgressPercentage.Equals(100))
{
System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(#"C:\Windows\Media\Windows Notify.wav");
_sound.Play();
DialogResult _dialogResult = MessageBox.Show("No updates were available!", "No updates");
if (_dialogResult == DialogResult.OK)
{
this.Hide();
this.Close();
}
}
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_timer.Enabled = true;
_timer.Tick += new EventHandler(Timer_Tick);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 100; i++)
{
backgroundWorker1.ReportProgress(i);
System.Threading.Thread.Sleep(100);
}
}
void Timer_Tick(object sender, EventArgs e)
{
_timer.Enabled = false;
}
class CheckConnection
{
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
public static bool IsConnectedToInternet()
{
int Desc;
return InternetGetConnectedState(out Desc, 0);
}
}
Your answer much appreciated!
Thank you very much!
Because you call backgroundWorker1.RunWorkerAsync(); in the event WelcomeScreen_Load or in the event CheckUpdates_Load without checking if the Internet is connected or not. (Probably the worker_ProgressChanged is defined at design time withing the backgroundWorkder properties)
These events doesn't seems to be related to a Form.Load event handler because your class doesn't derive from Form. However it seems clear that you need to put a check there otherwise whoever triggers these events will start your background worker.
private void CheckUpdates_Load(object sender, EventArgs e)
{
if(CheckConnection.IsConnectedToInternet())
backgroundWorker1.RunWorkerAsync();
}
private void WelcomeScreen_Load(object sender, EventArgs e)
{
if(CheckConnection.IsConnectedToInternet())
backgroundWorker1.RunWorkerAsync();
}

c# windows form

I have a handler that returns some string messages. How can i put them into a form application? I mean I would like each time the handler is activated to print the result in a windows form. I now i need to use thread. I don't know how can i programatically create and add changes the name of the forms that pop up when a message is handled. Can someone please tell me how to do it?
I asssume that you have some work you want to do in the background and whenever an amount of progress is don you want to update the form...
Then I would use the backgroundworker:
BackgroundWorker bw = new BackgroundWorker();
public Form1()
{
InitializeComponent();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; (i <= 10); i++)
{
if ((worker.CancellationPending == true))
{
e.Cancel = true;
break;
}
else
{
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500);
worker.ReportProgress((i * 10)); //Activating the progressChanged event
}
}
}
Whenever you execute worker.ReportProgress() the event below will fire on the UI thread which is giving you the dynamic updates you wanted
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.tbProgress.Text = (e.ProgressPercentage.ToString() + "%");
}
When the worker is finished executing the RunWorkerCompleted event will fire
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
this.tbProgress.Text = "Canceled!";
}
else if (!(e.Error == null))
{
this.tbProgress.Text = ("Error: " + e.Error.Message);
}
else
{
this.tbProgress.Text = "Done!";
}
}
Source

Communicating with another thread

This is how I create a thread that does domething reps times:
protected virtual void RedButtonClicked(object sender, System.EventArgs e)
{
Nuker n = new Nuker(target, reps);
bombThread = new Thread(new ThreadStart(n.nuke));
bombThread.Start();
}
The thread class:
public class Nuker
{
private string target;
private int reps;
//...
public void nuke()
{
for(int i=0; i<reps; ++i)
{
ICBM.nuke(target);
Thread.Sleep(5500);
}
System.Console.WriteLine("Done.");
}
}
(I create a new class to store some variables since I can't pass these in ThreadStart().)
Now I would like to have a simple visualisation of the process, let's say printing the current repetition in a text field on a form. How would I use the i from the loop to do that?
In the simplest form you proved a callback in the Nuker class
public Nuker(string target, int reps, Action reportCallback){..}
In the loop you just call reportCallback(i);
Nuker n = new Nuker(target, reps, ReportMethod);
with
private void ReportMethod(int currentIdx)
{
if (InvokeRequired) // Invoke if UI update
...
}
But, probably you want to use the BackgroundWorker that has build in methods for reporting progress on the UI thread. Just check the examples on MSDN.
you can do it with a backgroundworker, it´s one of the easiest threads :-)
below i have post you a sample that i have createt to teach some friends the use of the backgroundworker ;-)
private BackgroundWorker bw = new BackgroundWorker();
public Form1()
{
InitializeComponent();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
}
public void buttonStart_Click(object sender, EventArgs e)
{
if (bw.IsBusy != true)
bw.RunWorkerAsync(12); //Start
}
public int Pils(int i)
{
Thread.Sleep(2000);
bw.ReportProgress(70, "In the middle of the work..");
Thread.Sleep(2000);
bw.ReportProgress(90, "Returning the result..");
Thread.Sleep(2000);
return (2 * i);
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
bw.ReportProgress(20, "Waiting for cancel..");
Thread.Sleep(2000);
if ((bw.CancellationPending == true))
e.Cancel = true;
else
{
bw.ReportProgress(50, "Starting process..");
e.Result = Pils((int)e.Argument);
}
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
bw.ReportProgress(100, "Work done..");
if ((e.Cancelled == true))
textBox1.Text = "Canceled!";
else if (e.Error != null)
textBox1.Text = ("Error: " + e.Error.Message);
else textBox1.Text = e.Result.ToString();
}
private void buttonCancel_Click(object sender, EventArgs e)
{
if (bw.WorkerSupportsCancellation == true)
bw.CancelAsync();
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
listBox1.Items.Add((e.ProgressPercentage.ToString() + "%") + " - " + e.UserState as String);
}
url to the blogpost: link

BackgroundWorker Not working in VSTO

I have a background worker. Before I invoke the worker I disable a button and make a gif visible. I then invoke the runworkerasync method and it runs fine until comleteion. On the 'RunWorkerCompleted()' I get a cross thread error. Any idea why?
private void buttonRun_Click(object sender, EventArgs e)
{
if (comboBoxFiscalYear.SelectedIndex != -1 && !string.IsNullOrEmpty(textBoxFolderLoc.Text))
{
try
{
u = new UpdateDispositionReports(
Convert.ToInt32(comboBoxFiscalYear.SelectedItem.ToString())
, textBoxFolderLoc.Text
, Properties.Settings.Default.TemplatePath
, Properties.Settings.Default.ConnStr);
this.buttonRun.Enabled = false;
this.pictureBox1.Visible = true;
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.RunWorkerAsync();
//backgroundWorker1.RunWorkerAsync();
}
catch (Exception ex)
{
MessageBox.Show("Unable to process.\nError:" + ex.Message, Properties.Settings.Default.AppName);
}
}
}
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
buttonRun.Enabled = true;
pictureBox1.Visible = false;
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
u.Execute();
}
It seems to be an issue with VSTO and BackgroundWorker.
The solution is here.
Basically you need to call
System.Threading.SynchronizationContext.SetSynchronizationContext(new WindowsFormsSynchronizationContext());
before you call RunWorkerAsync. Works great.
To avoid instantiating the object every time you may have a static member in you AddIn's main class and reuse it. This way you only instantiate once.
something about VSTO running the background worker on the same thread as the controls. Not sure. I had to check the InvokeRequired
private void buttonRun_Click(object sender, EventArgs e)
{
if (comboBoxFiscalYear.SelectedIndex != -1 && !string.IsNullOrEmpty(textBoxFolderLoc.Text))
{
try
{
u = new UpdateDispositionReports(
Convert.ToInt32(comboBoxFiscalYear.SelectedItem.ToString())
, textBoxFolderLoc.Text
, Properties.Settings.Default.TemplatePath
, Properties.Settings.Default.ConnStr);
this.buttonRun.Enabled = false;
this.pictureBox1.Visible = true;
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.RunWorkerAsync();
//backgroundWorker1.RunWorkerAsync();
}
catch (Exception ex)
{
MessageBox.Show("Unable to process.\nError:" + ex.Message, Properties.Settings.Default.AppName);
}
}
}
delegate void ReenableRunCallback();
private void ReenableRun()
{
if (this.buttonRun.InvokeRequired)
{
ReenableRunCallback r = new ReenableRunCallback(ReenableRun);
this.buttonRun.Invoke(r, null);
}
else
this.buttonRun.Enabled = true;
}
private void HideProgress()
{
if (this.pictureBox1.InvokeRequired)
{
ReenableRunCallback r = new ReenableRunCallback(HideProgress);
this.pictureBox1.Invoke(r, null);
}
else
this.pictureBox1.Visible = false;
}
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
ReenableRun();
HideProgress();
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
u.Execute();
}

Categories

Resources