Windows Media Player 9 in a Multi-threaded Class

Does anyone knows how to launch a Windows Media Player 9 control (to play a movie file) from a Multi-threaded class?

I tried to create an instance of the Windows Media Player ActiveX control in my class as below,

m_objMediaPlayer = New AxWMPLib.AxWindowsMediaPlayer()

but the following error occured:-

Run-time exception thrown : System.Threading.ThreadStateException - Could not instantiate ActiveX control '6bf52a52-394a-11d3-b153-00c04f79faa6' because the current thread is not in a single-threaded apartment.

I have tried the following method too, the Media Player control is displayed and the movie is playing, but I can't get the PlayState and Open State of the Media Player control :-

m_objMediaPlayer = New WMPLib.WindowsMediaPlayer()

Please help, thanks.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ASP.Net threads are MTA by default. So you can't instatiate a single threaded controls or components without changing the threading model to STA or Aparemtent threaded. There are 2 ways to do it.

1. in your page attribute, set aspCompat to "true".
2. You can setthe same attribute programtically in your page's Init module.
 Page.AspCompat = true
3. or you can change the current thread's ApartmentState to STA.

Thread.CurrentThread.ApartmentState = ApartmentState.SAT

For windows apps, you will have to make sure that main function has STA attribute set for apartment type or right at the begining of app you can change the threading mode.
sinloongAuthor Commented:
Thanks naveenkohli, FYI, I am doing this for windows application in VB.NET, not ASP.NET.

I have set the apartment type to single threaded attribute right at the begining of my component, but it didn't work. I am still getting the same error message when calling the following code :-

m_objMediaPlayer = New AxWMPLib.AxWindowsMediaPlayer()
sinloongAuthor Commented:
nvm, I have found my problem, I have to create another new thread to make sure that the thread is in a thread safe fashion.
COuld you please take care of this open question so that it can be moved to PAQ?


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.