.NET 收到 ThreadStateException [英] .NET getting a ThreadStateException

查看:20
本文介绍了.NET 收到 ThreadStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在以下行中收到错误消息:

Hello I am getting an error on the following line:

If Not System.Windows.Clipboard.GetDataObject Is Nothing Then

我相信它在 C# 中看起来像这样

I believe it would look something like this in C#

if (System.Windows.Clipboard.GetDataObject!=null) {

错误是:

ThreadStateException:在进行 OLE 调用之前,必须将当前线程设置为单线程单元 (STA) 模式."

"ThreadStateException: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made."

谁能告诉我如何解决这个问题?一些在线建议提到添加 ;_ 'main' 方法,但是,这是一个 ASP.NET 控制器方法,我尝试在它上面添加它,但没有帮助.有人有什么建议吗?

Can anyone tell me how to fix this? A few suggestions online mentioned adding <STAThread()> _ over the 'main' method, however, this is an ASP.NET controller method, I tried adding this over it and it didn't help. Anyone have any suggestions?

推荐答案

ASP.NET 与 STAThread 并不真正相处.

ASP.NET doesn't really get along with STAThread.

你需要一个额外的任务调度器来在 STA 模式下运行一个线程来访问你想要的操作系统

You need an additional task scheduler to run a thread in STA mode to access what you want from the operating system

看看这篇文章它让我去做这样的事情.

Take a look at this article it got me going with something like this.

另外,请记住,对您的页面的多个请求可能会造成混乱,因为您正在与操作系统的剪贴板(在另一个 COM 上下文中运行)交互,并发可能存在风险.

Also, keep in mind that multiple request to your page might mess up things, since you're interacting with the clipboard of the operating system (which runs in another COM context) son concurrency might by risky.

你想完成什么,也许有更好的方法.

What are you trying to acomplish, maybe there is a better way.

这篇关于.NET 收到 ThreadStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆