Link to home
Start Free TrialLog in
Avatar of sinloong
sinloong

asked on

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()
m_objMediaPlayer.OpenPlayer("c:\MTV.dat")

Please help, thanks.
Avatar of naveenkohli
naveenkohli

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.
Avatar of sinloong

ASKER

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()
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.
ASKER CERTIFIED SOLUTION
Avatar of naveenkohli
naveenkohli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial