当前位置:网站首页>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) |
边栏推荐
- Allusions of King Xuan of Qi Dynasty
- Embedded -arm (bare board development) -1
- 网站页面禁止复制内容 JS代码
- 激动人心!2022开放原子全球开源峰会报名火热开启!
- 机器学习02:模型评估
- Learnopongl notes (II) - Lighting
- ThoughtWorks global CTO: build the architecture according to needs, and excessive engineering will only "waste people and money"
- 精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
- CMake教程Step2(添加库)
- dried food! Semi supervised pre training dialogue model space
猜你喜欢

WSL2.0安装

The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale

阈值同态加密在隐私计算中的应用:解读

飞桨EasyDL实操范例:工业零件划痕自动识别

【729. 我的日程安排錶 I】
MySQL queries the latest qualified data rows

【性能测试】jmeter+Grafana+influxdb部署实战

Application of threshold homomorphic encryption in privacy Computing: Interpretation

CMake教程Step2(添加库)

ECU introduction
随机推荐
Little knowledge about C language (array and string)
高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
goto Statement
深耕5G,芯讯通持续推动5G应用百花齐放
【testlink】TestLink1.9.18常见问题解决方法
[Web attack and Defense] WAF detection technology map
【二叉树】根到叶路径上的不足节点
浏览器渲染原理以及重排与重绘
WSL2.0安装
mysql如何使用JSON_EXTRACT()取json值
thinkphp3.2.3
Error in compiling libssh2. OpenSSL cannot be found
NPM installation
基于51单片机的电子时钟设计
Wechat official account web page authorization login is so simple
ECU introduction
2022 年 Q2 加密市场投融资报告:GameFi 成为投资关键词
编译libssh2报错找不到openssl
【性能测试】jmeter+Grafana+influxdb部署实战
Timestamp strtotime the day before or after the date