当前位置:网站首页>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) |
边栏推荐
- C# TCP如何限制单个客户端的访问流量
- 【性能测试】全链路压测
- 【729. 我的日程安排表 I】
- C#实现水晶报表绑定数据并实现打印3-二维码条形码
- 域名解析,反向域名解析nbtstat
- CMake教程Step1(基本起点)
- Embedded-c Language-2
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
- Tips for extracting JSON fields from MySQL
- C language to get program running time
猜你喜欢

Use JDBC technology and MySQL database management system to realize the function of course management, including adding, modifying, querying and deleting course information.

Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city

Etcd build a highly available etcd cluster
Learn about MySQL transaction isolation level
MYSQL group by 有哪些注意事项

33:第三章:开发通行证服务:16:使用Redis缓存用户信息;(以减轻数据库的压力)

拷贝方式之DMA

Embedded-c Language-2
MySQL queries the latest qualified data rows

一个满分的项目文档是如何书写的|得物技术
随机推荐
CMake教程Step3(添加库的使用要求)
thinkphp模板的使用
Embedded-c Language-1
CMake教程Step2(添加库)
ternary operator
Jarvis OJ Telnet Protocol
Allusions of King Xuan of Qi Dynasty
C# TCP如何限制单个客户端的访问流量
Embedded UC (UNIX System Advanced Programming) -1
npm安装
CMake教程Step1(基本起点)
干货!半监督预训练对话模型 SPACE
PHP人才招聘系统开发 源代码 招聘网站源码二次开发
SQL删除重复数据的实例教程
Wechat official account web page authorization login is so simple
[7.7 live broadcast preview] the lecturer of "typical architecture of SaaS cloud native applications" teaches you to easily build cloud native SaaS applications. Once the problem is solved, Huawei's s
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%
URP下Alpha从Gamma空间到Linner空间转换(二)——多Alpha贴图叠加
Iphone14 with pill screen may trigger a rush for Chinese consumers