The current thread is not in a single thread unit, so the ActiveX control cannot be instantiated. Solution

Source: Internet
Author: User

(1) cited classics (msdn ):

1. A unit is a logical container of objects within a process that have the same thread access requirements. All objects in the same unit can receive calls from any thread in the unit .. . NET Framework does not use units. The managed object is responsible for using all shared resources in a thread-safe manner.

Because the com class uses units, the public Language Runtime Library must call COM in the case of COM InterOP.
Object To create and initialize a unit. A managed thread can create and enter a single-threaded unit (STA) that only allows one thread or contains one or more threads.
(MTA ). Set the apartmentstate attribute of the thread to apartmentstate.
One of the enumerated values, which can control the type of the created unit. Given that a thread can only initialize a com unit onceCodeYou cannot change the unit type.

2. Public Enum apartmentstate

Member
MTA thread creates and enters a multi-threaded unit.
The STA thread creates and enters a single thread unit.
Unknown has not set the apartmentstate attribute.

Http://www.cnblogs.com/winzheng/archive/2009/05/11/1454328.html

3. sample:

 

Code
Using System;
Using System. Threading;

class apartmenttest
{< br> static void main ()
{< br> thread newthread = New thread ( New threadstart (threadmethod);
// apartmentstate property is obsolete;
The following statements are critical
newthread. setapartmentstate (apartmentstate. MTA);

// The following line is ignored since
// Apartmentstate can only be set once.
Newthread. setapartmentstate (apartmentstate. Sta );
Console. writeline ( " Threadstate: {0}, apartmentstate: {1} " ,
Newthread. threadstate, newthread. apartmentstate );
Newthread. Start ();
// Wait for newthread to start and go to sleep.
Thread. Sleep ( 300 );
Try
{
// This causes an exception since newthread is sleeping.
Newthread. setapartmentstate (apartmentstate. Sta );
}
Catch (Threadstateexception stateexception)
{
Console. writeline ( " \ N {0} caught: \ n "   +
" Thread is not in the unstarted or running state. " ,
Stateexception. GetType (). Name );
Console. writeline ( " Threadstate: {0}, apartmentstate: {1} " ,
Newthread. threadstate, newthread. getapartmentstate ());
}
}

Static VoidThreadmethod ()
{
Thread. Sleep (1000);
}
}

 

(2) Causes of other methods:

[Permissionset (securityaction. Demand, name = "fulltrust")]
[System. runtime. interopservices. comvisible (true)]

If you add [stathread]:

[Stathread]
Is a thread model used inProgramTo specify
The apartmentstate is Sta. It does not affect other methods. On the ASPX page, you can use aspcompat = "true"
To achieve the same effect. This attribute is only useful in COM InterOP.
It is useless. [Stathread] indicates that the default thread model of an application is a single-threaded unit.
(STA ). The startup thread model can be set to single-threaded or multi-threaded. If it is not set, the thread is not initialized. That is to say, if you use. net
And does not use com InterOP. Generally, this attribute is not required. Others include MTA (multi-threaded suite) and free
Thread (free thread ).

(3) specific environment conditions

Recently, I am working on a simple Screen Saver. The function is very simple, that is, when the machine is not used for a long time, start one at a certain interval.
Screen Saver. I want to play some media files (. Avi,. wmv) and Flash files, so I use Windows built-in
Windowsmediaplayer is used as a player.
For some special reasons, I need to start the screen saver in a program without a GUI, so the system. threading. timer is used as the timer, but it is found that the following exceptions are always present during the test:
Message: because the current thread is not in a single thread unit, the ActiveX Control "6bf52a52-394a-11d3-b153-00c04f79faa6" cannot be instantiated ".
Source: system. Windows. Forms
At system. Windows. Forms. axhost .. ctor (string CLSID, int32 flags)
At system. Windows. Forms. axhost .. ctor (string CLSID)
At axwmplib. axwindowsmediaplayer .. ctor ()
...
This is from a brother's blog.
After a long time, I found that the original windowsmediaplayer control can only be run in a single-threaded environment. Later I changed my timer to system. windows. forms. timer object, and then test. Everything is OK! Conclusion: I found that I did not read the development documents of Ms well when I was writing some programs through this error. I did not pay attention to thread security. I should pay special attention to this issue during the next development.

(4) to be supplemented.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.