|
-
December 9th, 2011, 09:53 AM
#1
Background Worker...how to kill the class??
Hello! First Post!
I am a C# relative newbie; I picked up this code from MSDN:
*******************************************
class AreaClass2
{
public double Base;
public double Height;
public double CalcArea()
{
// Calculate the area of a triangle.
return 0.5 * Base * Height;
}
}
private System.ComponentModel.BackgroundWorker BackgroundWorker1
= new System.ComponentModel.BackgroundWorker();
private void TestArea2()
{
InitializeBackgroundWorker();
AreaClass2 AreaObject2 = new AreaClass2();
AreaObject2.Base = 30;
AreaObject2.Height = 40;
// Start the asynchronous operation.
BackgroundWorker1.RunWorkerAsync(AreaObject2);
}
private void InitializeBackgroundWorker()
{
// Attach event handlers to the BackgroundWorker object.
BackgroundWorker1.DoWork +=
new System.ComponentModel.DoWorkEventHandler(BackgroundWorker1_DoWork);
BackgroundWorker1.RunWorkerCompleted +=
new System.ComponentModel.RunWorkerCompletedEventHandler(BackgroundWorker1_RunWorkerCompleted);
}
private void BackgroundWorker1_DoWork(
object sender,
System.ComponentModel.DoWorkEventArgs e)
{
AreaClass2 AreaObject2 = (AreaClass2)e.Argument;
// Return the value through the Result property.
e.Result = AreaObject2.CalcArea();
}
private void BackgroundWorker1_RunWorkerCompleted(
object sender,
System.ComponentModel.RunWorkerCompletedEventArgs e)
{
// Access the result through the Result property.
double Area = (double)e.Result;
MessageBox.Show("The area is: " + Area.ToString());
}
*************************************************
I put the background worker code behind a form push button. So when I press the button I
get the result via the messagebox..fantastic...but press the button again, I get the result twice,
press it again, I get it three times...etc
Obviously the class I create with each push button still remains after worker completed, and thus I create an additional class with each button push.
How do I "kill" the class when backgroundworker worker completes? If I cant, how can I avoid creating a new class if I already have one from a previous button push...
Yes Newb questions, but thanks for your help.
-
December 9th, 2011, 10:36 AM
#2
Re: Background Worker...how to kill the class??
Don't worry about creating a new instance every time. .NET will clean up automatically for you (that's what working in a managed language is all about).
You could always make the instance of the class a member variable and this will allow the background worker to reuse the instance. But beware ! If you ended up with multiple background workers (i.e. more than one thread at a time referencing the data) you hit concurrency issues and will have to start thinking about sychronising.
Synchronising sharing memory is a standard threading problem and if you're going to use threads I really recommend getting yourself familiar with them - it'll cause you so much less pain in future.
Darwen.
-
December 9th, 2011, 05:46 PM
#3
Re: Background Worker...how to kill the class??
In your button click call BackgroundWorker1.IsBusy to determine if you need to call RunWorkerAsync. Also, look at CancelAsync to cancel the current async operation (rather than killing or aborting the thread).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|