【发布时间】:2017-06-27 19:06:48
【问题描述】:
例如,我如何将 textBox UI 元素的文本传递给 Task.Run() 方法?此代码将引发异常(...其他线程拥有它)。当我通过过滤器变量时,异常消失了,这是因为字符串作为值传递了吗?
private async void filterTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (currentSession.SessionFilenames != null)
{
string filter = filterTextBox.Text;
dirListing.ItemsSource = await Task<IEnumerable<string>>.Run(()=> Filterfiles(filterTextBox.Text));
}
}
【问题讨论】:
-
是的,就是这样
-
基本上,是的 :-) 阅读更多内容here。但相反的情况并非如此:文本框失败是因为文本框是 control on the UI thread,而不是因为它通过 ref 与 val 传递。
标签: c# multithreading xaml async-await task-parallel-library