How do I use backgroundworker in this instance? - c#

I've a question on how the backgroundworker would actually work. I'm not too sure how it would work with different methods.
So for example I have the following code (adapted from http://broadcast.oreilly.com/2010/06/understanding-c-using-backgrou.html for illustration purposes):
private void getDateTime()
{
DateTime startTime = DateTime.Now;
double value = Math.E;
while (DateTime.Now < startTime.AddMilliseconds(100))
{
value /= Math.PI;
value *= Math.Sqrt(2);
}
}
private backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for(int i=0; i <= 100; i++)
{
getDateTime();
backgroundWorker1.ReportProgress(i);
}
}
This would continually calculate the DateTime using the backgroundworker thread. Now I'm assuming that the for-loop within the backgroundworker's DoWork method would run the calculation, report the progress, and then reiterate the loop (please do correct me if I've misunderstood).
Now assuming I have another method, which parses a (presumably large) DataTable and returns another DataTable, using it in the backgroundworker would mean I'd be parsing the DataTable over and over (theoretically. again, correct me if I'm wrong)? If this is how the program would flow, is there a more efficient method to achieve the DataTable parsing without having to reiterate the loop, but still being able to use backgroundworker?
This is intended for a Windows Form, and the UI tends to freeze up when I parse the DataTable(s). I've heard that backgroundworker could help me get around the freezing of the UI, but I find it highly inefficient to have to re-parse the entire DataTable over, and over. However I'm not sure of any other way I can report progress without a for-loop either.
EDIT:
I've figured out a temporary solution. It works, but I still have no way of reporting progress.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int rows = (int)e.Argument;
int count = 0;
t3 = runComparison();
for (int i = 0; i <= rows; i++)
{
count++;
int current = count / rows;
backgroundWorker1.ReportProgress(current * 100);
}
e.Result = t3;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show("An unexpected error has occurred. Please try again later.", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
dataGridView1.DataSource = e.Result;
}
}
I'm aware my for-loop doesn't do anything. However, the "runComparison" already consumes a lot of time. As such, progress doesn't get reported until an entire run-through if it's in the for-loop. Alternatively, maybe I could use a label or messagebox instead of the progress bar. Not too sure how I'm supposed to achieve this.

Try to do this:
//make sure background worker report progress
backgroundWorker1.WorkerReportsProgress = True;
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
progressBar1.Refresh;
//if your progressbar is on a status strip, you need to refresh the status strip instead
}

I've come up with a rather inelegant solution. But it satisfies the requirement for user-friendliness so... Whatever works :P
Thanks for all the feedback, I appreciate it.
Anyway, here's the code:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
backgroundWorker1.ReportProgress(1); //to invoke ProgressChanged
t3 = runComparison(); //run intensive method
e.Result = t3; //return DataTable
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Visible = true; //display a label
textBoxProgress.Visible = true; //display the progress bar
//change the progress bar style
//the Marquee style is constantly running, so it could show users the process is working
//not too sure how to explain it, you'll have to try it out.
progressBar1.Style = ProgressBarStyle.Marquee;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show("An unexpected error has occurred. Please try again later.", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
//reset the display and update the DataGridView
progressBar1.Visible = false;
textBoxProgress.Visible = false;
progressBar1.Style = ProgressBarStyle.Blocks;
dataGridView1.DataSource = e.Result;
}
}

Related

Why can I not recursively call a Worker_DoWork function?

I have a tcp server and client in c# using Sytem.Net.Sockets. My worker function is as following
private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
byte[] buffer = new byte[1];
socket.Receive(buffer);
chatTextBox.Text = buffer[0].ToString();
}
I'm calling this after the server is created, as soon as it receives a message from the client it writes it on the screen and stops. My issue is I want it at the end to call itself again so it waits for another message to display. If I just add worker.RunWorkerAsync(); at the bottom it doesn't work, however if I just call it from another button it works and receives and writes the message.
The BackgroundWorker "wraps" around a Thread and helps you with all the plumbing. It is a horribly dated approach to Multitasking taht you should not use in producive code anymore. However it is also the best "Training Wheels" for Multitasking I know off.
Your fundamental approach is flawed. The BGW not allowing that is really just it helping you learn the things you need to learn. A list of mistakes in your code:
you are accessing a GUI element directly in DoWork. Do not do that. Only write the UI in ReportProgress and RunWorker completed Events. This is a general Multithreading rule, that is helped by teh BGW's design.
you are trying to restart the thread, before it has finished. If you wanted to restart it, RunWorkerCompleted would be the right place
however more sensible would be for the core of the BackgroundWorker to be a semi-infinite loop. Something that runs until canceled. Normally handing out inforamtion with reporting is not a good idea, but in this case it is the best idea I have.
The only other thing I can give you, is my old BGW example code:
#region Primenumbers
private void btnPrimStart_Click(object sender, EventArgs e)
{
if (!bgwPrim.IsBusy)
{
//Prepare ProgressBar and Textbox
int temp = (int)nudPrim.Value;
pgbPrim.Maximum = temp;
tbPrim.Text = "";
//Start processing
bgwPrim.RunWorkerAsync(temp);
}
}
private void btnPrimCancel_Click(object sender, EventArgs e)
{
if (bgwPrim.IsBusy)
{
bgwPrim.CancelAsync();
}
}
private void bgwPrim_DoWork(object sender, DoWorkEventArgs e)
{
int highestToCheck = (int)e.Argument;
//Get a reference to the BackgroundWorker running this code
//for Progress Updates and Cancelation checking
BackgroundWorker thisWorker = (BackgroundWorker)sender;
//Create the list that stores the results and is returned by DoWork
List<int> Primes = new List<int>();
//Check all uneven numbers between 1 and whatever the user choose as upper limit
for(int PrimeCandidate=1; PrimeCandidate < highestToCheck; PrimeCandidate+=2)
{
//Report progress
thisWorker.ReportProgress(PrimeCandidate);
bool isNoPrime = false;
//Check if the Cancelation was requested during the last loop
if (thisWorker.CancellationPending)
{
//Tell the Backgroundworker you are canceling and exit the for-loop
e.Cancel = true;
break;
}
//Determin if this is a Prime Number
for (int j = 3; j < PrimeCandidate && !isNoPrime; j += 2)
{
if (PrimeCandidate % j == 0)
isNoPrime = true;
}
if (!isNoPrime)
Primes.Add(PrimeCandidate);
}
//Tell the progress bar you are finished
thisWorker.ReportProgress(highestToCheck);
//Save Return Value
e.Result = Primes.ToArray();
}
private void bgwPrim_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pgbPrim.Value = e.ProgressPercentage;
}
private void bgwPrim_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
pgbPrim.Value = pgbPrim.Maximum;
this.Refresh();
if (!e.Cancelled && e.Error == null)
{
//Show the Result
int[] Primes = (int[])e.Result;
StringBuilder sbOutput = new StringBuilder();
foreach (int Prim in Primes)
{
sbOutput.Append(Prim.ToString() + Environment.NewLine);
}
tbPrim.Text = sbOutput.ToString();
}
else
{
tbPrim.Text = "Operation canceled by user or Exception";
}
}
#endregion

Events not happening in order

I am creating a WinForms application in C#. When I click a button, a certain flow of events is supposed to transpire:
Click Button
Show label1
Show label2
Call function to parse a string the user entered before (this can take awhile depending on the string)
Show listBox1 and progressBar1
backgroundWorker1.RunWorkerAsync
backgroundWorker1_DoWork() does something x number of times and reports progress each time
backgroundWorker1_ProgressChanged() updates progressBar1 and adds an item to listBox1
backgroundWorker1_RunWorkCompleted() shows a message box saying "DONE"
But that is not what actually happens. When I trace through the code and look at the form it has several problems.
label1 and label2 do not actually appear until after the parsing is done.
progressBar1 only sometimes gets updated as ProgressChanged gets called. Other times it will wait until after "DONE" is printed and update all at once.
Each time progressChange() gets called the vertical scroll bar on listBox1 gets smaller so I can tell Items are being added, but the text of the Items does not appear until after "DONE" is printed.
I am new to using backgroundWorker, so it's possible I just don't understand how it is supposed to function. But the delay of showing the labels I just don't understand at all. There are no errors when I trace through the code and the lines appear to be executed in the correct order.
Does anyone have ideas about what could be causing these issues? I would appreciate any help or advice. I'd rather not post my code, just because there is kind of a lot, but if anyone needs it to better understand, just lmk.
EDIT: Here is the code.
private void button1_Click(object sender, EventArgs e){
label1.Show();
label2.Show();
String errMsg = parseString();
if (errMsg == ""){
listBox1.Items.Clear();
listBox1.Show();
progressBar1.Maximum = 100;
progressBar1.Step = 1;
progressBar1.Value = 0;
progressBar1.Show();
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
if (backgroundWorker1.IsBusy != true)
{
backgroundWorker1.RunWorkerAsync();
}
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
backgroundWorker1.ReportProgress(1, "Updating Devices");
for (int i = 0; i < 100; i++)
{
//todo: do stuff
//update progress
backgroundWorker1.ReportProgress(i, "Device:" + i);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
listBox1.Items.Add(e.UserState);
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("DONE");
}
Thanks to #HansPassant and #mjwills for their comments. They led me on the right track and made this solution possible.
In the end I decided to do two background workers to solve the issue of label1 and label2 not appearing until after the parsing was done. I use the first one to do the parsing and the second one to do the "do stuff" section. In the code you will see I had to use Invoke to edit the labels since that part now existed on a different thread.
I also realized that the "do stuff" before calling ProgressChanged is not immediate. I've been developing in pieces and hadn't yet implemented that code, but I know it will take at least 3 seconds for those actions to complete (partly because pinging is involved). So for now I have put a Sleep(3000) call in that loop to simulate how it will actually behave. This solved the weird progressbar1 and listbox1 behavior which was caused by eating up all the memory.
Here is how the code turned out:
private void button1_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy != true)
{
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
String errMsg = parseString();
if (errMsg == "")
{
if (listBox1.InvokeRequired)
{
listBox1.Invoke(new MethodInvoker(delegate
{
listBox1.Items.Clear();
listBox1.Show();
}));
}
if (progressBar1.InvokeRequired)
{
progressBar1.Invoke(new MethodInvoker(delegate
{
progressBar1.Maximum = 100;
progressBar1.Step = 1;
progressBar1.Value = 0;
progressBar1.Show();
}));
}
if (backgroundWorker2.IsBusy != true)
{
backgroundWorker2.RunWorkerAsync();
}
}
else
{
MessageBox.Show(errMsg);
}
}
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
backgroundWorker2.ReportProgress(1, "Updating Devices");
for (int i = 0; i < 100; i++)
{
System.Threading.Thread.Sleep(3000);
//do stuff
backgroundWorker2.ReportProgress(i, "Device:" + i);
}
}
private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (progressBar1.InvokeRequired)
{
progressBar1.Invoke(new MethodInvoker(delegate
{
progressBar1.Value = e.ProgressPercentage;
}));
}
if (listBox1.InvokeRequired)
{
listBox1.Invoke(new MethodInvoker(delegate
{
listBox1.Items.Add(e.UserState);
}));
}
}
private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("DONE");
}

Progressbar does not move (WinForms, .Net and BackgroundWorker)

Introduction
I am trying to make a WinForms app using .Net.
I am using tutorial from here wich shows BackgroundWorker and ProgressBar integration.
I added ProgressBar and BackgroundWorker controls to the form.
The names are the same as in example. Additionaly, i set WorkerReportProgress property to True for BackgroundWorker. No errors are shown and project compiles sucessfully...
Problem
The problem is - progressbar does not move.
And yet, it moves when clicked manually... progressBar1.PerformStep();.
What am i missing?
Code
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, System.EventArgs e)
{
// Start the BackgroundWorker.
BackgroundWorker1.RunWorkerAsync();
}
private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= 100; i++)
{
// Wait 500 milliseconds.
Thread.Sleep(500);
// Report progress.
BackgroundWorker1.ReportProgress(i);
}
}
private void BackgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// Change the value of the ProgressBar to the BackgroundWorker progress.
progressBar1.Value = e.ProgressPercentage;
// Set the text.
this.Text = e.ProgressPercentage.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
progressBar1.PerformStep();
}
}
}
Update
Removed progressBar1.PerformStep(); from DoWork and ProgressChanged.
Still the problem persists (ProgressBar does not move).
Thank you for ideas so far, will look into it more on Monday.
After you made sure you attached the event handlers to ProgressChanged and DoWork:
Remove progressBar1.PerformStep() from DoWork event handler.
Then use just progressBar1.Value = e.ProgressPercentage; in ProgressChanged event handler.
I wrote a simple Multithreading with Progress bar code a few years back. Hope it helps you:
#region Primenumbers
private void btnPrimStart_Click(object sender, EventArgs e)
{
if (!bgwPrim.IsBusy)
{
//Prepare ProgressBar and Textbox
int temp = (int)nudPrim.Value;
pgbPrim.Maximum = temp;
tbPrim.Text = "";
//Start processing
bgwPrim.RunWorkerAsync(temp);
}
}
private void btnPrimCancel_Click(object sender, EventArgs e)
{
if (bgwPrim.IsBusy)
{
bgwPrim.CancelAsync();
}
}
private void bgwPrim_DoWork(object sender, DoWorkEventArgs e)
{
int highestToCheck = (int)e.Argument;
//Get a reference to the BackgroundWorker running this code
//for Progress Updates and Cancelation checking
BackgroundWorker thisWorker = (BackgroundWorker)sender;
//Create the list that stores the results and is returned by DoWork
List<int> Primes = new List<int>();
//Check all uneven numbers between 1 and whatever the user choose as upper limit
for(int PrimeCandidate=1; PrimeCandidate < highestToCheck; PrimeCandidate+=2)
{
//Report progress
thisWorker.ReportProgress(PrimeCandidate);
bool isNoPrime = false;
//Check if the Cancelation was requested during the last loop
if (thisWorker.CancellationPending)
{
//Tell the Backgroundworker you are canceling and exit the for-loop
e.Cancel = true;
break;
}
//Determin if this is a Prime Number
for (int j = 3; j < PrimeCandidate && !isNoPrime; j += 2)
{
if (PrimeCandidate % j == 0)
isNoPrime = true;
}
if (!isNoPrime)
Primes.Add(PrimeCandidate);
}
//Tell the progress bar you are finished
thisWorker.ReportProgress(highestToCheck);
//Save Return Value
e.Result = Primes.ToArray();
}
private void bgwPrim_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pgbPrim.Value = e.ProgressPercentage;
}
private void bgwPrim_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
pgbPrim.Value = pgbPrim.Maximum;
this.Refresh();
if (!e.Cancelled && e.Error == null)
{
//Show the Result
int[] Primes = (int[])e.Result;
StringBuilder sbOutput = new StringBuilder();
foreach (int Prim in Primes)
{
sbOutput.Append(Prim.ToString() + Environment.NewLine);
}
tbPrim.Text = sbOutput.ToString();
}
else
{
tbPrim.Text = "Operation canceled by user or Exception";
}
}
#endregion
Normally when writing a UI Element from a Alterante Thread, you have to use Invoke. BackgroundWorker is nice and Invoking the "ReportProgress" and "RunWorkerCompleted" Events on the thread that created it (wich should be the GUI thread) so you do not have to deal with that part of Multithreading wonkyness yet.
It is also nice enough to catch any Exceptions that would normally escape DoWork and be swallowed, exposing them to you in the Completed Event Args. Swallowing Exceptions is a huge issue with Multithreading.
The core issue is, that your loop breaks due to a Exception. Calling progressBar1.PerformStep(); inside the DoWork Event has to throw a "CrossThreadException". The BackgroudnWorker finishes (due to an exception) instantly. The RunWorker completed event is triggered when i was just the initial value.

ProgressBar while finding a file

Hi I am trying to show a progress bar while in background the code is looping till it finds a file with specific name.
I have written following code for that but the progress bar values doesn't change.
What should I change in the code below?
public partial class Form1 : Form
{
BackgroundWorker backgroundWorker1 = new BackgroundWorker();
public Form1()
{
InitializeComponent();
progressBar1.Visible = false;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork +=
new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged +=
new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(backgroundWorker1_WorkDone);
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
progressBar1.Visible = true;
}
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Your background task goes here
for (int i = 0; i <= 100; i++)
{
// Report progress to 'UI' thread
backgroundWorker1.ReportProgress(i);
// Simulate long task
while (!File.Exists(#"C:\Users\Test.txt"))
{
continue;
}
}
}
void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
// The progress percentage is a property of e
progressBar1.Value = e.ProgressPercentage;
}
void backgroundWorker1_WorkDone(object sender,
RunWorkerCompletedEventArgs e)
{
progressBar1.Visible = false;
}
}
The continue in your code will simply continue to the next iteration of the while(true) loop. It won't go back up to the for loop as you expect - it just keeps looping in there over and over again. If it wasn't a background worker, it'd hang your entire program. Since it is, it just hangs that thread. I expect that one CPU core stays at 100% while this is running.
That being said, while the goal here is admirable, there's no good way to accomplish it. Even if you fix the infinite loop, your progress bar will either "finish" at a low percentage (1%, 2%, 3%, done) or go up to 100, then stop updating, but without the file yet existing.

progressbar not progressing using backgroundworker

I am using a background worker to update some tables in sqlserver. the progressbar max is getting set to the correct value, the progressbar value is being incremented, the backgroundworker progresschanged is being called correctly with correct value, yet the bar is not progressing.
here is the code for the form
in the background_dowork method there is a loop which calls updateProgressBarValue which works with correct values.
public InterfaceConvertLonLat()
{
InitializeComponent();
Shown += new EventHandler(Form1_Shown);
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
}
public void ConvertLonLat_Load(object sender, EventArgs e)
{
}
public void updateProgressBarValue()
{
progressBar1.Value++;
backgroundWorker1.ReportProgress(progressBar1.Value);
}
public void setProgressBarMax(int max)
{
progressBar1.Maximum = max;
MessageBox.Show("setprogressbarmax " + max);
}
public void Form1_Shown(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
convert.OSGB36ToWGS84("paf");
}
public void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
here is the loop contained in another class which calls updateprogressbarvalue, this is being fired and as stated backgroundworker1_progressChanged is being fired but the bar is not moving.
con.setProgressBarMax(address.Tables[0].Rows.Count);
foreach (DataRow LonLat in address.Tables[0].Rows)
{
con.updateProgressBarValue();
Double lon = 0;
Double lat = 0;
lat = Convert.ToDouble(LonLat["LTO"]);
lon = Convert.ToDouble(LonLat["LGO"]);
LocalToWGS84(ref lat, ref lon, OGB_M);
cmd1.Parameters["#LTW"].Value = lat;
cmd1.Parameters["#LGW"].Value = lon;
string dbQuery1 = "update " + tableName + " set LTW = #LTW, LGW = #LGW";
cmd1.CommandText = (dbQuery1);
cmd1.CommandType = CommandType.Text;
cmd1.Connection = conn;
cmd1.ExecuteNonQuery();
}
}
catch (Exception e)
{
MessageBox.Show("error converting: " + e.Message);
}
finally
{
conn.Close();
}
When reporting progress you need to fire an event from the background worker to tell the progressbar it needs updating. This can be done using the below:
backgroundWorker1.ReportProgress(10);
Change the value to what you need to demonstrate increased progress in your code. The progress changed event will mostly run on the same thread as your GUI so you should have no cross thread issues. One exception is if your form is being called from Excel via addin in which case Excel will be on the main thread.
You've got a number of issues - Your UpdateprogressBarValue() just increases the value of the progress bar but doesn't keep track of how many times its been called / what the current value is - so if you call it 101 times (assuming a range of 0-100), you'll get an OutOfRangeException
Your DoWork() method doesn't seem to call the update at all (either directly or by raising an event).
You can use events to do this but you're better off using delegates or perhaps anonymous functions. Something like...
public void setProgress(int value) {
progressBar1.invoke(delegate{ progressBar1.Value = value; }
}
then just call setProgress(0) through setProgress(progressBar1.MaxValue) from your DoWork() method
The following is seriously wrong:
public void updateProgressBarValue()
{
progressBar1.Value++; // not thread-safe
backgroundWorker1.ReportProgress(progressBar1.Value);
}
ReportProgress() is intended to be called form DoWork, it should not even read a Control property.
You should maintain a counter in the foreach loop and feed that to the progress mechanism.
Now this does not directly indicate why it doesn't move but you do not have a Completed handler. Are you sure the process finishes at all?
If an exception escapes your DoWork you will never know what happened.
private void Form1_Load(object sender, System.EventArgs e)
{
// Start the BackgroundWorker.
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= 100; i++)
{
// Wait 100 milliseconds.
Thread.Sleep(100);
// Report progress.
backgroundWorker1.ReportProgress(i);
}
}
private void backgroundWorker1_ProgressChanged(object sender, progress e)
{
// Change the value of the ProgressBar to the BackgroundWorker progress.
progressBar1.Value = e.ProgressPercentage;
// Set the text.
this.Text = e.ProgressPercentage.ToString();
}
I think this might be caused by the fact that the UI in windows forms is running in its own thread/context. In this case you probably need to use Invoke to make adjustments to the UI.
Something like this:
public void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Invoke(new ProgressDelegate(UpdateProgress), e.ProgressPercentage);
}
delegate void ProgressDelegate(decimal value);
private void UpdateProgress(decimal value)
{
progressBar1.Value = value;
}

Categories

Resources