Avatar of rgn2121
rgn2121Flag for United States of America

asked on 

Question regarding the correct use of the BackgroundWorker and the way I use it.

For simplicity I have 2 classes, one that has the Mainform and the other that handles most of the work for that form.  In the main form, I call the a method in my class that does a few things and then kicks off the BGW to pull oracle data.
When the BGW is complete (RunWorkerCompleted), I return the dataset from (e.result) to my main form by raising an event which is caught by code in the main form.  Herein lies the issue.  When the dataset is returned, I call another routine in a different class and pass the dataset to it for processing.

I am worried that my bgw never fully gets to complete.  I believe because of the "RaiseEvent" , it continues running the rest of my code.  The only way I see to actually allow it to stop is to get to a point where code execution stops and User input is required.  The way I currently have the app designed is that the user makes selections on what they want to see, data is pulled, manipulated and then spit out to a report.  At that pooint execution is stopped.  Seems like a bad thing to me, but I am a little new at this and maybe there is something I have missed.

Thoughts?
.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
rgn2121
ASKER CERTIFIED SOLUTION
Avatar of kaylanreilor
kaylanreilor
Flag of Luxembourg image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of rgn2121
rgn2121
Flag of United States of America image

ASKER

Just worried that the rest of my app is now being completed by the background worker and it never gets recovered by garbage collection until the app closes.  If I step through code, in the RunWorkerCompleted,  I do the following..
If e.cancelled = False then
bgw.dispose
RaiseEvent WorkerComplete
...
So, the workercompleted is hit and the e.cancelled is checked, generally this is false, so it goes and does the dispose and reports to the mainform that the work has completed.  Then the code runs in the main form until all application execution stops.  Once this happens it will go back to the End if, End sub.  
I was just curious if this is a problem.  Everything operates as I want and I don't see any issues, but the real RunWorkerCompleted never reaches End Sub until all other execution is finished.
Avatar of kaylanreilor
kaylanreilor
Flag of Luxembourg image

It's not that there is a problem... there is not enough info to state that.
Nevertheless, I just don't understand why you want to dispose the BGW as soon as it tolds you that is work is completed.
The BackgroundWorker itself should be disposed IF RELEVANT but that's something you do from the thread that created the background worker in the first place. If you added a BackgroundWorker to your form using the designer there is no need to dispose it, the generated code will take care of it.
Moreover, classes which implement Dispose() method propose it to close or release unmanaged resources such as files, streams, and handles.
Another example:
http://msdn.microsoft.com/en-us/library/hybbz6ke.aspx
Avatar of rgn2121
rgn2121
Flag of United States of America image

ASKER

Nevertheless, I just don't understand why you want to dispose the BGW as soon as it tolds you that is work is completed.
I dispose of everything and set things to nothing if applicable as soon as I am finished with them.
If you added a BackgroundWorker to your form using the designer there is no need to dispose it, the generated code will take care of it.
I didn't, I created it in code as the Background Worker is not part of form, it is only in a class library for the application.
Moreover, classes which implement Dispose() method propose it to close or release unmanaged resources such as files, streams, and handles.
Another example
:
The class doesn't implement the Dispose() method as it does not inherit from a class that implements Dispose() nor do I implement IDisposable because I don't necessarily need it as I just set the instance of the class equal to nothing if used.  The elements that deal with the BGW are through shared Methods and as such don't require an instance of the class.
I have since changed the logic of the application which will allow all execution to complete as it normally would.  I appreciate the assistance.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo