在哪里可以找到 ThreadPool.SwitchTo 方法?

发布于 2024-10-14 16:34:46 字数 528 浏览 7 评论 0原文

我正在研究新的异步 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

折戟 2024-10-21 16:34:46

作为参考,CharlesO 在上面的评论中回答了他的问题:

好的,找到了,摘要:提供
与交互的方法
线程池。备注:ThreadPoolEx是一个
占位符。

公共共享函数 SwitchTo() As
System.Runtime.CompilerServices.YieldAwaitable
成员
System.Threading.ThreadPoolEx摘要:
创建一个等待
异步产生
等待时的线程池。

For reference, CharlesO answered his question within the comments above:

OK, found it guys, Summary: Provides
methods for interacting with the
ThreadPool. Remarks: ThreadPoolEx is a
placeholder.

Public Shared Function SwitchTo() As
System.Runtime.CompilerServices.YieldAwaitable
Member of
System.Threading.ThreadPoolEx Summary:
Creates an awaitable that
asynchronously yields to the
ThreadPool when awaited.

¢好甜 2024-10-21 16:34:46

ThreadPool.SwitchTo 被删除可能是因为它是一个 anit 模式。考虑一下如果在切换回原始上下文之前抛出异常会发生什么。您不能使用finally 块作为防范该异常并切换回来的对策,因为await 不能出现在finally 块中。

public async void button1_Click(object sender, EventArgs e) 
{ 
  await ThreadPool.SwitchTo();
  try
  {
    // Do something dangerous here.
  }
  finally
  {
    await button1.Dispatcher.SwitchTo(); // COMPILE ERROR!
  }

}

当然,您可以开发自定义的可等待和等待者类型以准确实现已删除的内容。但是,使用 Task.Run1 比通过 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 a finally block as a counter measure to guard against that exception and switch back because await cannot appear in finally blocks.

public async void button1_Click(object sender, EventArgs e) 
{ 
  await ThreadPool.SwitchTo();
  try
  {
    // Do something dangerous here.
  }
  finally
  {
    await button1.Dispatcher.SwitchTo(); // COMPILE ERROR!
  }

}

You can, of course, develop custom awaitable and awaiter types to achieve exactly what was removed. However, it is far better to use Task.Run1 than to switch contexts midstream via await.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文