views:

41

answers:

2

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.AxAnimation", on execution i get

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

On search i found a link :Forum Link

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.

+1  A: 

You cannot use a BackgroundWorker to animate this control. It is an ActiveX control, it requires that you create it in your app's main thread. The same thread that creates the form.

This is a very common restriction for controls in general, they do not support threading.

The linked thread does not solve your problem. Yes, you can create your own STA thread by calling SetApartmentState() and specifying STA but now you cannot do anything with the Form that you created on the main thread. You'll get an exception when you add the control to the form. The child controls of a form must be created in the same thread as the form.

This should in general not be an issue. The animation ought to be reasonably smooth as long as you keep the form's event handlers short and snappy. If such an event handler needs to do something that takes a while (and freezes the animation as a result) then let a BGW perform that job.

Hans Passant
A: 

You need to use a delegate and have the main thread actually control the animation. You invoke the delegate from the background thread but it runs in the main thread. This page, second post, has the rough idea: http://www.vbforums.com/showthread.php?t=377205

Chris Haas