当前位置:网站首页>C (WinForm) the current thread is not in a single threaded unit, so ActiveX controls cannot be instantiated
C (WinForm) the current thread is not in a single threaded unit, so ActiveX controls cannot be instantiated
2022-07-05 17:16:00 【coder i++】
Reprint :https://blog.csdn.net/qq_37508511/article/details/82770349
1、 Instantiate this in the main thread ActiveX Control
2、 This will be created Active The thread of the control is set to single thread .
Thread.ApartmentState Gets or sets the cell state of this thread .
thread = new Thread(Mehtod);
thread.SetApartmentState(System.Threading.ApartmentState.STA);
thread.Start();
Or thread pool
ThreadPool.QueueUserWorkItem(new WaitCallback(functionname),obj);
If not in a single threaded unit , Then declare it as a single threaded unit
to Main Function plus [STAThread]
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
}
}
stay .NET Framework 1.0 Version and 1.1 Version of the ,ApartmentState Property marks the thread as indicating that it will execute in a single threaded unit or a multi-threaded unit . When the thread is in Unstarted or Running In thread state , You can set this property ; But a thread can only set this property once . If this property has not been set , It will return Unknown.
stay .NET Framework 2.0 Version of the , If their unit state has not been set before starting new threads , Then these new threads will be initialized to ApartmentState.MTA. By default , The main application thread is initialized to ApartmentState.MTA. You can no longer set System.Threading.ApartmentState Property sets the main application thread to ApartmentState.STA. Should be used instead STAThreadAttribute.
stay .NET Framework 1.0 and 1.1 Version of the , By using System.Threading.ApartmentState Property sets the unit state after the thread runs ; This will create a race condition . stay .NET Framework 2.0 Version of the , This is not allowed .
COM Components use units to synchronize access to resources . In contrast , Managed objects use synchronization zones 、 Synchronize primitives and context to ensure that all shared resources are used in a thread safe manner , Where synchronous primitives refer to mutexes 、 Lock and completion port and other objects . of .NET Other information about the threading model , See managed thread processing .
For interoperability , The common language runtime will call COM Object to create a unit and initialize it . Managed threads can create and enter single threaded units that contain only one thread (STA) Or a multithreaded unit containing one or more threads (MTA). When COM When the unit is compatible with the unit generated by the thread ,COM Will allow the calling thread to call directly COM object . If the two units are incompatible ,COM A compatible unit will be created and all calls will be marshaled in the new unit through the proxy .
Runtime calls CoInitializeEx take COM Unit initialized to MTA or STA unit . stay .NET Framework 2.0 Version of the , If the unit state of the managed thread is not set before starting the thread , Then the managed thread will be initialized to MTA. Before starting the thread, you can use SetApartmentState or TrySetApartmentState Method to set the unit state .
stay .NET Framework 2.0 Version of the , Unless STAThreadAttribute Apply to entry point processes , Otherwise, the main application thread will be initialized to MTA.
The following table lists ApartmentState Enumerate values and display comparable COM Unit initialization call .
ApartmentState Enumerated values | COM Unit initialization |
|---|---|
MTA | CoInitializeEx(NULL, COINIT_MULTITHREADED) |
STA | CoIntializeEx(NULL, COINIT_APARTMENTTHREADED) |
Unknown | CoInitializeEx(NULL, COINIT_MULTITHREADED) |
边栏推荐
猜你喜欢
随机推荐
齐宣王典故
Error in compiling libssh2. OpenSSL cannot be found
ThoughtWorks global CTO: build the architecture according to needs, and excessive engineering will only "waste people and money"
拷贝方式之DMA
C#(Winform) 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件
Iphone14 with pill screen may trigger a rush for Chinese consumers
American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields
What else do you not know about new map()
goto Statement
Function sub file writing
菜刀,蚁剑,冰蝎,哥斯拉的流量特征
Wsl2.0 installation
[first lecture on robot coordinate system]
Use of ThinkPHP template
国内首家 EMQ 加入亚马逊云科技「初创加速-全球合作伙伴网络计划」
域名解析,反向域名解析nbtstat
CMake教程Step1(基本起点)
EasyX second lesson
WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
Is it safe to open an account for digging wealth stocks? How is it safe to open a stock account?






