在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式,请确保您的Main函数带有STAThreadAttribute标记。

本文介绍在多线程环境下使用SaveFileDialog组件时遇到的问题及解决方案,详细解释了如何设置线程为单线程单元(STA)模式,以避免出现调用OLE前必须设置线程模式的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考自:http://www.cnblogs.com/winzheng/archive/2008/12/02/1345656.html

出错环境:

多线程接收文件的时候,需要打开一个SaveFileDialog对象,并调用ShowDialog()方法

出错代码:

void threadProc()
{
    ...
    SaveFileDialog saveFileDlg = new SaveFileDialog();
    if (saveFileDlg.ShowDialog() != DialogResult.OK)
    {
         /返回之后,recvThreadProc
         return;
    }
    ...
}
出现异常:

当执行到saveFileDlg.ShowDialog()方法的时候,就会出现如下错误提示:“在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式,请确保您的Main函数带有STAThreadAttribute标记。”

出错原因:

确认了下,Main()函数之前确实有STAThreadAttribute标记,因此不是该问题,而是当前线程的问题!

        [STAThread]
        static void Main()
        {}
据之前的了解,由于该操作在新创建的线程中执行,应该是不能访问UI的,也就是说直接调用ShowDialog()方法确实是有问题的。

按照“多线程中安全的访问控件”一文中提到的方法的话,必须创建一个委托,然后异步调用该委托才可以

但是,异步调用委托的方法似乎比较麻烦...

既然问题是出在了“将当前线程设置为单线程单元(STA)模式”这块儿,所以就想着从这块入手

解决方法:

通过将线程的 ApartmentState 属性设置为ApartmentState 枚举值之一,可以控制所创建的单元的类型。

其中:

STA:Thread 将创建并进入一个单线程单元;

MTA:Thread 将创建并进入一个多线程单元;

Unknown:尚未设置 ApartmentState 属性。

代码如下:

Thread recvThread=new Thread(new ParameterizedThreadStart(recvThreadProc));
recvThread.SetApartmentState(ApartmentState.STA);
recvThread.Start(commSock);



Rebuild started: Project: Project *** Using Compiler 'V6.22', folder: 'E:\Keil_v5\ARM\ARMCLANG\Bin' Rebuild target 'Target 1' assembling startup_stm32f10x_md.s... Start/core_cm3.c(445): error: non-ASM statement in naked function is not supported 445 | uint32_t result=0; | ^ Start/core_cm3.c(442): note: attribute is here 442 | uint32_t __get_PSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(465): error: parameter references not allowed in naked functions 465 | "BX lr \n\t" : : "r" (topOfProcStack) ); | ^ Start/core_cm3.c(461): note: attribute is here 461 | void __set_PSP(uint32_t topOfProcStack) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(479): error: non-ASM statement in naked function is not supported 479 | uint32_t result=0; | ^ Start/core_cm3.c(476): note: attribute is here 476 | uint32_t __get_MSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(499): error: parameter references not allowed in naked functions 499 | "BX lr \n\t" : : "r" (topOfMainStack) ); | ^ Start/core_cm3.c(495): note: attribute is here 495 | void __set_MSP(uint32_t topOfMainStack) __attribute__( ( naked ) ); | ^ 4 errors generated. compiling core_cm3.c... compiling misc.c... compiling system_stm32f10x.c... compiling stm32f10x_adc.c... compiling stm32f10x_dac.c... compiling stm32f10x_exti.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_dma.c... compiling stm32f10x_crc.c... compiling stm32f10x_cec.c... compiling stm32f10x_bkp.c... compiling stm32f10x_can.c... compiling stm32f10x_flash.c... compiling stm32f10x_pwr.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值