Hi,
I got a while loop which makes the Form act slow. I got advised to use a backgroundWorker.
Yesterday i read on the msdn library what it is and how it kinda works.
Unfortunately, i didn't figure it all out. I was hoping you guys might wanna help me out.
This is my while loop:
this.formBreakLoop = false;
while (!this.formBreakLoop)
{
int numberChosen = rnd.Next(0, words.Length - 1);
this.lblDisplayWord.Text = words[numberChosen].ToString();
this.lblDisplayWord.Update();
Application.DoEvents();
Thread.Sleep(4500);
this.lblDisplayWord.Text = string.Empty;
this.lblDisplayWord.Update();
Thread.Sleep(500);
}
//constructor
public Form3()
{
InitializeComponent();
InitializeBackgoundWorker();
}
And these are the methods i copied from msdn library:
private System.ComponentModel.BackgroundWorker backgroundWorker1;
private void InitializeBackgoundWorker()
{
this.backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Get the BackgroundWorker that raised this event.
BackgroundWorker worker = sender as BackgroundWorker;
// Assign the result of the computation
// to the Result property of the DoWorkEventArgs
// object. This is will be available to the
// RunWorkerCompleted eventhandler.
//e.Result = ComputeFibonacci((int)e.Argument, worker, e);
}
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)
{
// Next, handle the case where the user canceled
// the operation.
// Note that due to a race condition in
// the DoWork event handler, the Cancelled
// flag may not have been set, even though
// CancelAsync was called.
//resultLabel.Text = "Canceled";
}
else
{
// Finally, handle the case where the operation
// succeeded.
//resultLabel.Text = e.Result.ToString();
}
}
Am i suppose to place my while loop in one of these methods
Or do i have to call it in the while loop
And which one would i call in the while loop
Thanks in advance!

BackgroundWorker
cjg6300
Thanks for your reply.
Its confusing indeed. There are like 3 methods i added and i don't know in which one i should be putting the code which is in the while loop right now.
I didn't find any other good sample codes to see how i can do it in mine.
I would be great full if you made an example with my code.
I tried a few things, its not working for me.
Thanks in advance!
Michael J. Brown
The idea is to put all the code of long running process into a Function and call that function from DoWork Event of backgrond worker. So this event is must to handle. Then call the funciton BackgroundWorker.RunWorkerAsync(); from anywhere in your code as in your case you are doing it from Constructor.
Put the following code in a seperate function and see ComputeFibonacci((int)e.Argument, worker, e); You need to call your on function like this; see your example above:
this.formBreakLoop = false;
while (!this.formBreakLoop)
{
int numberChosen = rnd.Next(0, words.Length - 1);
this.lblDisplayWord.Text = words[numberChosen].ToString();
this.lblDisplayWord.Update();
Application.DoEvents();
Thread.Sleep(4500);
this.lblDisplayWord.Text = string.Empty;
this.lblDisplayWord.Update();
Thread.Sleep(500);
}
The above function is not suitable for work like this u need to remove Update and Asignement code from here and where Raise ProgressChanged Event like this
worker.ReportProgress(percentComplete, SomeObject);
in percent you can always pass 0 because you dont need that, Use second parameter for your actual work. You can have some object here which holds data about your work like in the above example you can pass string...
In the Progress Changed handler you'll get e.ObjectState or e.ProgressPercentage and do what ever you need to do with them.
I know its seems a little bit confuzing when you use BackgroundWorker for the first time but its simple to use. Using the above explaination I hope i'll be able to manage this and will proceed towards the solution.
Need More help Feel free to Write here again!
Best Regards,
JohnWilliams
Hi,
Thank you!
I'll try that!
Guruprasad H R
Here:
public
partial class FormMain : Form{
private string words; Random rnd; public FormMain(){
InitializeComponent();
rnd =
new Random();words =
"hfjkhdsafk lhjk hdsafjkhjk hdsfjkhdsfdsafreajkheaulfhjklhdsafjkl hdsafjklhdsafukewhruflhklafhdsahjklhdsaflgrefhlfjdbsamnbdsam,bvjdsafbjdsafbjdsa"; // Supose this is text you read form the File this.backgroundWorkerLoad.RunWorkerAsync(words); // Start Asynchronous work}
private void backgroundWorkerLoad_DoWork(object sender, DoWorkEventArgs e){
string words = Convert.ToString(e.Argument); // Get the argument which was passed to this.backgroundWorkerLoad.RunWorkerAsync(words);LoadToLabel(words); // Run the Time Consuming Code in a function
}
private void backgroundWorkerLoad_ProgressChanged(object sender, ProgressChangedEventArgs e){
this.lblDisplayWord.Text = Convert.ToString(e.UserState); // get the data passed by ReportProgress Method}
private void LoadToLabel(string words) // Time consuming work{
while (!this.backgroundWorkerLoad.CancellationPending){
int numberChosen = rnd.Next(0, words.Length - 1); this.backgroundWorkerLoad.ReportProgress(0, words[numberChosen].ToString()); // Report Progress Thread.Sleep(2000); this.backgroundWorkerLoad.ReportProgress(0, "") ; // Report Progress Thread.Sleep(500);}
}
private void btnCancel_Click(object sender, EventArgs e){
this.backgroundWorkerLoad.CancelAsync(); // Cancel the Task = while() condition}
I hope this will help you now ;-)
Best Regards,