use*_*245 0 c# clipboard asynchronous winforms
我在通过异步方法获取剪贴板文本时遇到问题。因为它总是返回一个空值(虽然它不为空)。这是问题的简单演示:
private async void button_Click(object sender, EventArgs e)
{
string result = await Task<string>.Run(() =>
{
System.Threading.Thread.Sleep(3000);
return Clipboard.GetText(); //returns empty! (but clipboard is not empty)
});
MessageBox.Show(result);
}
Run Code Online (Sandbox Code Playgroud)
我确信剪贴板不为空。解决办法是什么?
小智 5
它不起作用,因为剪贴板仅在 是 而您的公寓是COM threading model (apartment)时才起作用。您无法更改任务的单元,但可以使用线程代替。线程有一个方法。STATaskMTASetApartmentState
但我找到了创建 STA 任务的解决方案!
技巧是使用任务运行 STA 线程:
public static Task<T> StartSTATask<T>(Func<T> func)
{
var tcs = new TaskCompletionSource<T>();
var thread = new Thread(() =>
{
try
{
var result = func();
tcs.SetResult(result);
}
catch (Exception e)
{
tcs.SetException(e);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
return tcs.Task;
}
Run Code Online (Sandbox Code Playgroud)
所以现在你可以让它像这样工作:
private async void button1_Click(object sender, EventArgs e)
{
var result = await StartSTATask(() =>
{
Thread.Sleep(3000);
return Clipboard.GetText();
});
MessageBox.Show(result);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
691 次 |
| 最近记录: |