当前位置:网站首页>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) |
边栏推荐
- Using C language to realize palindrome number
- PHP人才招聘系统开发 源代码 招聘网站源码二次开发
- Understand the usage of functions and methods in go language
- 兰空图床苹果快捷指令
- Embedded-c Language-1
- Zhang Ping'an: accélérer l'innovation numérique dans le cloud et construire conjointement un écosystème industriel intelligent
- 【剑指 Offer】66. 构建乘积数组
- Deeply cultivate 5g, and smart core continues to promote 5g applications
- 【beanshell】数据写入本地多种方法
- [Jianzhi offer] 62 The last remaining number in the circle
猜你喜欢
npm安装
MYSQL group by 有哪些注意事项
Embedded-c Language-2
【Web攻防】WAF检测技术图谱
WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields
【剑指 Offer】63. 股票的最大利润
dried food! Semi supervised pre training dialogue model space
mysql中取出json字段的小技巧
Judge whether a string is a full letter sentence
随机推荐
33:第三章:开发通行证服务:16:使用Redis缓存用户信息;(以减轻数据库的压力)
Learn about MySQL transaction isolation level
微信公众号网页授权登录实现起来如此简单
2022 年 Q2 加密市场投融资报告:GameFi 成为投资关键词
MySQL queries the latest qualified data rows
thinkphp模板的使用
Wsl2.0 installation
Use byte stream to read Chinese from file to console display
【二叉树】根到叶路径上的不足节点
In depth understanding of redis memory obsolescence strategy
C#(Winform) 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件
Etcd build a highly available etcd cluster
基于51单片机的电子时钟设计
thinkphp3.2.3
What are the precautions for MySQL group by
How does the outer disk futures platform distinguish formal security?
ThoughtWorks global CTO: build the architecture according to needs, and excessive engineering will only "waste people and money"
【剑指 Offer】61. 扑克牌中的顺子
CMake教程Step1(基本起点)
机器学习编译第2讲:张量程序抽象