最近在用C#做backgroundWorker多线程读剪贴板内容时,突然读不到,原因是使用了多线程.一般作法是在方法加[STAThread]类型,但没用.用了其它方式解决了,直接上代码了:
public static string GetClipboard()
{
var txt = string.Empty;
RunInSta(() => { txt = Windows.Forms.Clipboard.GetText(); });
return txt;
}
public static void RunInSta(Action action)
{
Thread thread = new Thread(() => action());
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();//Wait for the thread to end
}
以上就是【C#多线程(backgroundWorker)读取不了剪贴板的解决办法】的全部内容了,欢迎留言评论进行交流!