当前位置:网站首页>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) |
边栏推荐
- 域名解析,反向域名解析nbtstat
- Writing method of twig array merging
- 叩富网开期货账户安全可靠吗?怎么分辨平台是否安全?
- 一文了解Go语言中的函数与方法的用法
- Allusions of King Xuan of Qi Dynasty
- 张平安:加快云上数字创新,共建产业智慧生态
- Detailed explanation of printf() and scanf() functions of C language
- 关于mysql中的json解析函数JSON_EXTRACT
- PHP talent recruitment system development source code recruitment website source code secondary development
- The first lesson of EasyX learning
猜你喜欢

First day of learning C language

URP下Alpha从Gamma空间到Linner空间转换(二)——多Alpha贴图叠加

Embedded UC (UNIX System Advanced Programming) -2

【729. 我的日程安排錶 I】

腾讯音乐上线新产品“曲易买”,提供音乐商用版权授权

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

Error in composer installation: no composer lock file present.

ternary operator

精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
随机推荐
Embedded UC (UNIX System Advanced Programming) -2
菜刀,蚁剑,冰蝎,哥斯拉的流量特征
[Jianzhi offer] 61 Shunzi in playing cards
dried food! Semi supervised pre training dialogue model space
Use of ThinkPHP template
C# TCP如何限制单个客户端的访问流量
一文了解Go语言中的函数与方法的用法
NPM installation
Etcd build a highly available etcd cluster
CMake教程Step2(添加库)
一文了解MySQL事务隔离级别
Rider 设置选中单词侧边高亮,去除警告建议高亮
2022 年 Q2 加密市场投融资报告:GameFi 成为投资关键词
thinkphp3.2.3
Embedded-c Language-1
深入理解Redis内存淘汰策略
Wechat official account web page authorization login is so simple
Embedded-c Language-5
【性能测试】jmeter+Grafana+influxdb部署实战
What else do you not know about new map()