Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Windows Media Player 9 in a Multi-threaded Class

Posted on 2003-02-24
Medium Priority
Last Modified: 2007-12-19
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.
Question by:sinloong
  • 2
  • 2
LVL 23

Expert Comment

ID: 8008180
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.

Author Comment

ID: 8022549
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()

Author Comment

ID: 8290274
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.
LVL 23

Accepted Solution

naveenkohli earned 2000 total points
ID: 8662142
COuld you please take care of this open question so that it can be moved to PAQ?


Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question