Results 1 to 6 of 6

Thread: [RESOLVED] Backgroundworker and objects that have event handlers

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    11

    Resolved [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

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    11

    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.

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    11

    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

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Backgroundworker and objects that have event handlers

    Yep... that sounds right...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width