VB 2008 find all running background workers

WJoeMoore
WJoeMoore used Ask the Experts™
on
Question about multiple background workers in VB.

They may be created by multiple classes.  I'm trying to create a function within an appliction that determines which, if any, of the background workers are busy.  Of course, I know how to query an individual one if I know it exists, but I want Windows to tell me which ones exist and whether or not they are busy.

Is there an object that will reveal that information to me?

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
This is for BGWs within your app right? You can get all open forms from Application.Open forms and then loop through its controls collection to see if there is any BGW.

For Each frm In Application.OpenForms
     For Each ctrl In frm.Controls
          If TypeOf(ctrl) Is BackGroundWorker Then
              Dim bgw As BackGroundWorker = Ctype(ctrl, BackGroundWorker)
              msgbox bgw.Name & ", " & bgw.IsBusy.ToString()
          End If
     Next
Next

Author

Commented:
That sounds like exactly what I need.  The only thing that doesn't compile is that Name doesn't seem to be a property of BackgroundWorker?
Most Valuable Expert 2012
Top Expert 2014

Commented:
It should be but try without it and see if it finds all the BGWs.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Author

Commented:
Hmmm.  For now, I'm just doing a bgw.tostring which should show the object type itself after casting it as a BackgroundWorker.  It's not picking it up, but I think that's because the background worker is not part of the form.  it's defined within a class which is part of the form.  Would that collection be somewhere else?

I know the rest of my code is working because the first thing the background worker does is add itself to a list and the last thing it does is removes itself from a list.  I then iterate through that list.  However, that leaves me the queasy feeling that I'll mess up that sequence somewhere.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Do you have one instance of the class in the form or multiple instances? You can access that one or collection of BGWs inside that form through the form reference in my code.
Top Expert 2011

Commented:
@WJoeMoore said:
I know the rest of my code is working because the first thing the background worker does is add itself to a list and the last thing it does is removes itself from a list.  I then iterate through that list.  However, that leaves me the queasy feeling that I'll mess up that sequence somewhere.

To avoid concurrency issues, you need to make sure all access to your list is synchronized.

Author

Commented:
@wdosanjos:  Thanks for the comment.

I suppose the way to take care of that would be to use a 'reportprogress' thread to add the item to the list.  Putting the removal on the dispose should take care of threading--right?  I don't get a threading exception--but of course that doesn't guarantee that there will never be a problem.

I'm acknowledging that my 'solution' is not good.  My question is:  How do I find all background processes that exist within a form class--even if they are not components on the form?
Top Expert 2011
Commented:
You can put the 'insert into list' code on your OnDoWork event and the 'remove from list' code on the dispose.  You need to make sure all operations on the List are syncronized (example below). This is to guarantee that only one threads accesses/updates the list at any given time, thus avoiding data corruption on the list.

lock(yourList)
{
    yourList.Add(yourBackgroundWorker);
}

To my knowledge, there is no way to inspect memory for objects of a given type (BackgroundWorker in your case).  Please refer to the post below:

http://stackoverflow.com/questions/302542/how-do-i-get-all-instances-of-all-loaded-types-that-implement-a-given-interface

Author

Commented:
'You can't do that' is always disappointing, but when it's correct and documented I can live with it.  You also covered some other important points.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial