我尝试了来自如何在C#中复制数据到剪贴板的代码:
Clipboard.SetText("Test!");
我遇到了这个错误:
在进行 OLE 调用前,必须将当前线程设置为单线程公寓 (STA) 模式。请确保您的
Main
函数已标记为STAThreadAttribute
。
如何修复它?
我尝试了来自如何在C#中复制数据到剪贴板的代码:
Clipboard.SetText("Test!");
我遇到了这个错误:
在进行 OLE 调用前,必须将当前线程设置为单线程公寓 (STA) 模式。请确保您的
Main
函数已标记为STAThreadAttribute
。
如何修复它?
Thread thread = new Thread(() => Clipboard.SetText("Test!"));
thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
thread.Start();
thread.Join(); //Wait for the thread to end
确保运行代码的线程标记有[STAThread]属性。对于WinForm和控制台应用程序,通常是Main
方法。
在你的主方法上方放置[STAThread]
:
[STAThread]
static void Main()
{
}
对于WinForms,通常可以在生成的Main.cs文件中进行编辑(如果需要,它不会在更改时重新生成)。对于控制台应用程序,您定义Main
的位置。
如果您无法控制线程(即您正在编写库或主应用程序由于某种原因被锁定),则可以在专门配置的线程上运行访问剪贴板的代码(.SetApartmentState(ApartmentState.STA)
),如另一个答案所示。
Main.cs
文件中定义...非常容易编辑。主方法与是否使用 WinForms 无关。顺便提一句,新项目从一开始就正确设置了此属性。 - Cody GrayMain.cs
。它直接放在您项目的根目录中。要进行编辑,您只需双击即可。我没有看到任何麻烦之处。 - Cody Gray你只能从STAThread访问剪贴板。
最快的解决方法是在Main()
方法之上放置[STAThread]
,但如果由于任何原因你不能这样做,你可以使用一个单独的类来创建一个STAThread并为你设置/获取字符串值。
public static class Clipboard
{
public static void SetText(string p_Text)
{
Thread STAThread = new Thread(
delegate ()
{
// Use a fully qualified name for Clipboard otherwise it
// will end up calling itself.
System.Windows.Forms.Clipboard.SetText(p_Text);
});
STAThread.SetApartmentState(ApartmentState.STA);
STAThread.Start();
STAThread.Join();
}
public static string GetText()
{
string ReturnValue = string.Empty;
Thread STAThread = new Thread(
delegate ()
{
// Use a fully qualified name for Clipboard otherwise it
// will end up calling itself.
ReturnValue = System.Windows.Forms.Clipboard.GetText();
});
STAThread.SetApartmentState(ApartmentState.STA);
STAThread.Start();
STAThread.Join();
return ReturnValue;
}
}
这个问题已经存在8年了,但仍有很多人需要解决方案。
正如其他人所提到的,剪贴板必须从主线程或[STAThread]中调用。
好吧,我每次都使用这个解决方法。也许它可以作为一种替代方案。
public static void SetTheClipboard()
{
Thread t = new Thread(() => {
Clipboard.SetText("value in clipboard");
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
await Task.Run(() => {
// use the clipboard here in another thread. Also can be used in another thread in another method.
});
}
剪贴板值是在t线程中创建的。
关键是:线程t的公寓设置为STA状态。
稍后,您可以随意在其他线程中使用剪贴板值。
希望您能理解。
Clipboard.SetText()
)吗?你能给我们提供更多的上下文信息吗(例如,该函数调用周围的代码)? - Cody Gray