|
-
Feb 9th, 2012, 02:46 PM
#1
Thread Starter
New Member
[RESOLVED] Backgroundworker and objects that have event handlers
I've started working with the backgroundworker class to try and keep the UI responsive while I make some expensive API calls. One of the API classes (details here[msdn.microsoft.com]) has its own event handler which I am using to update a progress bar in a small form that pops up. So I'm looking to use the PublishPackage command in a bgw thread and update a progress bar on the UI thread by using the ProgressHandler method.
Everything I've found talks about using the ReportProgress call to run the ProgressChanged routine but in my case there's no loop ... just one call to the API that could take minutes to complete.
I'm assuming the problem lies in the use of a single publisher object in both the UI and BGW threads but I'm not sure of how to mitigate that. Any ideas? I can throw up some pseudo code if it would help.
Thanks,
Bryan
-
Feb 9th, 2012, 02:54 PM
#2
Re: Backgroundworker and objects that have event handlers
make the progress bar marquee style, and set it to isrunning = true, then fire off your BGW... then when the process is complete, turn off the progress bar and move on...
the marquee style is the endless loop kind of thing... that's really all that will work... if you don't have progress to report, you can't update the prog bar any other way... think about it... if htere's no loop or no way to know it's half-way done, you can't set it to 50%...
-tg
-
Feb 9th, 2012, 03:22 PM
#3
Thread Starter
New Member
Re: Backgroundworker and objects that have event handlers
>if there's no loop or no way to know it's half-way done
Ahh, but there is. The IPublisher class I'm using has a event handler called ProgressHandler that tracks the progress of the PublishPackage method.
I have this working in a single thread but the dialog with the progress bar is unresponsive most of the time. So I'm trying to figure out how to call the PublishPackage method on a new thread while still using the ProgressHandler.
Yes, I could use a marquee style progress bar but this ProgressHandler gives insight into what the API is doing which is really useful when something goes wrong.
-
Feb 9th, 2012, 03:54 PM
#4
Re: Backgroundworker and objects that have event handlers
OK... so you need to declare an event with the same signarure, then pass the AddressOf to the ProgressHandler property... and then in the sub, get the progress value (presumably from the event args) and then you can use that to call the BGW's ReportProgress method (which I think takes a parameter that represents the % complete - so 50 would be 50%) ... and in the ProgressReport event handler of the BGW (I think I have that name right) set the Value of the Progress bar....
Now.. as for the setup... I think you should be able to instanciate your object in the DoWork event of the BGW, hook up the callback to the ProgressHandler of your IPublisher class... and so on...
yuck... sounds messy... but I think it might work..
-tg
-
Feb 9th, 2012, 04:32 PM
#5
Thread Starter
New Member
Re: Backgroundworker and objects that have event handlers
Thanks, that got me along the right path to getting it to work.
I assigned the sub for ProgressHandler in DoWork, had that sub call the BGW.ReportProgress (passing the sub's PublishingEventArgs as the UserState), and had ProgressChanged sub update the progress form using the passed object.
Thanks again,
Bryan
-
Feb 9th, 2012, 04:49 PM
#6
Re: Backgroundworker and objects that have event handlers
Yep... that sounds right...
-tg
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
|