Wpf 现场有人用过调度员吗?
在NDC 2012的“”中,向我介绍了建议使用的Wpf 现场有人用过调度员吗?,wpf,multithreading,Wpf,Multithreading,在NDC 2012的“”中,向我介绍了建议使用的Dispatcher.Yield。是否有人有关于如何(以及为什么)在野外使用此调用的示例(和解释)?例如,如果您有一项长期运行的任务,但仍需要更新UI,则可以使用Yield Yield使您能够离开当前线程上下文,并允许其他代码在底层上下文中运行 public async void MyButton_Click(object sender, RoutedEventArgs e) { for( int i=0; i < 10000; i+
Dispatcher.Yield
。是否有人有关于如何(以及为什么)在野外使用此调用的示例(和解释)?例如,如果您有一项长期运行的任务,但仍需要更新UI,则可以使用Yield
Yield
使您能够离开当前线程上下文,并允许其他代码在底层上下文中运行
public async void MyButton_Click(object sender, RoutedEventArgs e)
{
for( int i=0; i < 10000; i++)
{
ProcessSomeStuff(i);
// await the Yield to ensure all waiting messages
// are processed before continuing
await Task.Yield();
}
}
public async void MyButton\u单击(对象发送方,路由目标)
{
对于(int i=0;i<10000;i++)
{
加工材料(一);
//等待收益以确保所有等待的消息
//在继续之前已处理
等待任务;
}
}
在上面的示例中,您可以异步处理stuff,但调用
Yield
也将允许UI线程上的事件执行,例如,如果您有一个长时间运行的任务,但仍然需要更新UI,则可以使用Yield
Yield
使您能够离开当前线程上下文,并允许其他代码在底层上下文中运行
public async void MyButton_Click(object sender, RoutedEventArgs e)
{
for( int i=0; i < 10000; i++)
{
ProcessSomeStuff(i);
// await the Yield to ensure all waiting messages
// are processed before continuing
await Task.Yield();
}
}
public async void MyButton\u单击(对象发送方,路由目标)
{
对于(int i=0;i<10000;i++)
{
加工材料(一);
//等待收益以确保所有等待的消息
//在继续之前已处理
等待任务;
}
}
在上面的示例中,您可以异步处理stuff,但调用Yield
将允许UI线程上的事件也执行