Running animation in background worker

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Pakmarshal
    New Member
    • Aug 2009
    • 17

    #1

    Running animation in background worker

    Hi,

    I m developing an application in vb 2008, and using background worker to copy file(s). I have my own designed form which plays an animationon on the top of the form along with copy. For playing animation i m using "AxMSComCtl2.Ax Animation", on execution i get

    "ActiveX control '<Control ID>' cannot be instantiated because the current thread is not in a single-threaded apartment."

    On search i found a link : http://stackoverflow.com/questions/1...418471#1418471

    But still in this link Firstly i dont understand where to insert this snippet and secondly how to implement the idea on background worker.

    Please guide me in this regard.

    Thanks & Regards,

    Maverick.
  • ZiadElmalki
    New Member
    • Sep 2010
    • 43

    #2
    The example code is used when you are creating a thread. You are using the background worker so this is a thread pool thread not explicitly created by you.

    There is an attribute on the main method that defines if the main thread is sta or mta. This is a parameter is passed into CoInitializeEx which the CLR calls to initialize COM. This will not affect the thread pool thread though. I do not think you can change this for a thread pool thread.

    I have never used the video player control you are using but it probably has a dependency on the thread with the main message loop. Windows application has a thing called the message loop that processes messages from the OS. This loop is started via Application.Run or can be flushed by calling Application.DoE vents. Controls, forms, etc can not be access by any other threads besides the one they were created on. To update UI from other threads there is a property called IsInvokeRequire d and a method called Invoke on the Control class. IsInvokeRequire d tells you if the current thread is not the thread that owns the UI and Invoke is used to invoke a delegate on the UI thread.

    It seems like from the error message you are creating this control from the BackgroundWorke r.DoWork method. The DoWork method is executed on the thread pool thread and is unsafe to update UI from here unless you use Invoke and IsInvokeRequire d. The ProgessChangedE vent and CompletedEvent call Control.Invoke when they are raised so they are safe. If you can create this control here instead of DoWork. If possible pre layout the control on the form and just call Play or whatever method you need.

    Comment

    Working...