async Task Main 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件

.Net技术 码拜 4年前 (2020-03-14) 3575次浏览 0个评论

今天试用了一下 C# 7.1 的语法糖 ,在winform程序中为main方法使用 异步,在 Main() 里调用非同步 API,也就是 Main() 可以回传一个 Task,如下:
[STAThread]
private static async Task Main()

但是启动后,报以下错误:当前线程不在单线程单元中,因此无法实例化 ActiveX 控件“8856f961-340a-11d0-a96b-00c04fd705a2”


System.Threading.ThreadStateException
HResult=0x80131520
Message=当前线程不在单线程单元中,因此无法实例化 ActiveX 控件“8856f961-340a-11d0-a96b-00c04fd705a2”。
Source=System.Windows.Forms
StackTrace:
在 System.Windows.Forms.WebBrowserBase..ctor(String clsidString)
在 System.Windows.Forms.WebBrowser..ctor()
在 Spider.App.Forms.Draft.FrmEditArticleDraft.InitializeComponent() 在 E:\git\spider\Spider.App.CaptureUrl\Forms\Draft\FrmEditArticleDraft.Designer.cs 中: 第 45 行
在 Spider.App.Forms.Draft.FrmEditArticleDraft..ctor() 在 E:\git\spider\Spider.App.CaptureUrl\Forms\Draft\FrmEditArticleDraft.cs 中: 第 37 行
在 Spider.App.Forms.Draft.FrmEditArticleDraft.get_Instance() 在 E:\git\spider\Spider.App.CaptureUrl\Forms\Draft\FrmEditArticleDraft.cs 中: 第 51 行
在 Spider.App.Forms.FormStart.OpenDraftEdit() 在 E:\git\spider\Spider.App.CaptureUrl\Forms\FormStart.cs 中: 第 36 行
在 Spider.App.Forms.FormStart.FormStart_Load(Object sender, EventArgs e) 在 E:\git\spider\Spider.App.CaptureUrl\Forms\FormStart.cs 中: 第 30 行
在 System.Windows.Forms.Form.OnLoad(EventArgs e)
在 System.Windows.Forms.Form.OnCreateControl()
在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
在 System.Windows.Forms.Control.CreateControl()
在 System.Windows.Forms.Control.WmShowWindow(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
在 System.Windows.Forms.Form.WmShowWindow(Message& m)
在 System.Windows.Forms.Form.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


是项目里用了webbrowser的地方,实例化时报错。无奈改回原来的方式:

[STAThread]
private static void Main() ,然后调用异步的方法仍然用了wait()


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明async Task Main 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件
喜欢 (1)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!