在哪里可以找到 ThreadPool.SwitchTo 方法?
我正在研究新的异步 CTP 并浏览一些示例代码,
我遇到了这段代码:
public async void button1_Click(object sender, EventArgs e)
{
string text = txtInput.Text;
await ThreadPool.SwitchTo(); // jump to the ThreadPool
string result = ComputeOutput(text);
string finalResult = ProcessOutput(result);
await txtOutput.Dispatcher.SwitchTo(); // jump to the TextBox’s thread
txtOutput.Text = finalResult;
}
请问在哪里可以找到 ThreadPool.SwitchTo? SwithcTo 方法不在 ThreadPool 类上
我有对 AsyncCtpLibrary.dll 的引用...但没有运气
I am studying the new Async CTP and going through some sample code,
I came across this piece of code:
public async void button1_Click(object sender, EventArgs e)
{
string text = txtInput.Text;
await ThreadPool.SwitchTo(); // jump to the ThreadPool
string result = ComputeOutput(text);
string finalResult = ProcessOutput(result);
await txtOutput.Dispatcher.SwitchTo(); // jump to the TextBox’s thread
txtOutput.Text = finalResult;
}
Please where do I find ThreadPool.SwitchTo? SwithcTo method is not on the ThreadPool class
i have a referece to AsyncCtpLibrary.dll ... but no luck
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为参考,CharlesO 在上面的评论中回答了他的问题:
For reference, CharlesO answered his question within the comments above:
ThreadPool.SwitchTo 被删除可能是因为它是一个 anit 模式。考虑一下如果在切换回原始上下文之前抛出异常会发生什么。您不能使用
finally
块作为防范该异常并切换回来的对策,因为await
不能出现在finally
块中。当然,您可以开发自定义的可等待和等待者类型以准确实现已删除的内容。但是,使用
Task.Run
1 比通过await
中途切换上下文要好得多。ThreadPool.SwitchTo
was removed presumably because it is an anit-pattern. Consider what would happen if an exception were thrown before you switched back to the original context. You cannot use afinally
block as a counter measure to guard against that exception and switch back becauseawait
cannot appear infinally
blocks.You can, of course, develop custom awaitable and awaiter types to achieve exactly what was removed. However, it is far better to use
Task.Run
1 than to switch contexts midstream viaawait
.