Results 1 to 7 of 7

Thread: Threading WithEvents

  1. #1

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

    Question Threading WithEvents

    I have an application that performs a lot of busy work. All of the work is performed using a BackgroundWorker and the point of execution is the DoWork Event of the BackgroundWorker.

    On the UI thread, before the workload begins, I display a PictureBox that contains an animated GIF that illustrates progress; for the most part this works fine and the UI thread is not paused or slowed while the BackgroundWorker does its job. There is however, one instance where the UI thread is impacted by the work being done and it defeats the whole purpose of putting this work in a separate thread. In this application, the user must wait until the work completes before they can move to the next step, so my sole reason for moving this all to a BackgroundWorker was so that I could display a smooth, uninterrupted progress indicator on the UI thread while this work was being done, so that the user could see that something was happening.

    The case where the work being done is impacting the UI thread (pausing/delaying the PictureBox animation) deals with a referenced COM object.

    I have the following declared in my form:

    vb.net Code:
    1. Private WithEvents oCOMObject as X

    The COM object performs some processing when given a list of files and it is here where the majority of the lag takes place. In my BackgroundWorker's DoWork Event I have the following:

    vb.net Code:
    1. 'instantiate the COM object
    2. oCOMObject = New X
    3.  
    4. 'process the list of images (oFileList is a list of file paths on the system)
    5. oCOMObject.ProcessFiles (oFileList) 'it is here that the UI thread is delayed

    All of the COM object event handlers and subsequent functions are appropriately using Delegates, but I am not sure why the ProcessFiles method would cause the UI thread to hang while the work completes; I thought that was the whole purpose of the BackgroundWorker.

    Am I missing something? Any thoughts?

    Thanks!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading WithEvents

    What exactly does this mean?
    All of the COM object event handlers and subsequent functions are appropriately using Delegates
    If you're saying that you are invoking a method on the UI thread then that would likely explain the issue. How often are those event handlers executed and what exactly do they do?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

    Re: Threading WithEvents

    Here is a sample of one of the functions that is called from the oCOMObject event handler:
    vb.net Code:
    1. 'if an invoke is required
    2. If Me.InvokeRequired Then
    3.  
    4.     'invoke the function safely
    5.         Me.Invoke(New RecordCompleteDelegate(AddressOf RecordComplete), iRecordIndex)
    6.  
    7. Else 'on appropriate thread
    8.  
    9.     'perform work
    10.  
    11. End if'if an invoke is required

    Some of the work that needs to be done is on the UI thread, so I do invoke methods accordingly; in this case I would understand the lag, but it is the method call to .ProcessFiles that is impacting the UI thread, which I don't understand. The method takes about 2 seconds to complete and the UI is hung for those 2 seconds (prior to ANY event handling or invoking of any kind).

    Hope that helps explain what I mean.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading WithEvents

    In that case, I have no specific explanation. Maybe it's a COM thing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

    Re: Threading WithEvents

    Quote Originally Posted by jmcilhinney View Post
    In that case, I have no specific explanation. Maybe it's a COM thing.
    Not sure I have ever seen you say that ;o)

    Any other ideas here?

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Threading WithEvents

    It may be a COM thing... or something specific to the this COM thing... something tells me that because it's a COM component that it's going to want to run on the main thread and not the background one... but I don't think I could point to anything specific.

    -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??? *

  7. #7

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

    Re: Threading WithEvents

    Quote Originally Posted by techgnome View Post
    It may be a COM thing... or something specific to the this COM thing... something tells me that because it's a COM component that it's going to want to run on the main thread and not the background one... but I don't think I could point to anything specific.

    -tg
    Damn COM...wish I didn't have to use it!

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