【发布时间】:2016-03-26 09:02:48
【问题描述】:
我的程序有什么问题?我无法将执行线程与 UI 分开。在线程执行期间无法访问 UI。
这是我的视图模型:
第一个选项:
// on button click:
this.CreateImageList();
private void CreateImageList()
{
this.images.Clear();
ThreadStart threadStart = delegate
{
this.dispatcher.BeginInvoke(new ThreadStart(delegate
{
foreach (var filePath in this.fileBuffer)
{
var result = new ImageObject(filePath);
if (result.Image != null)
{
this.images.Add(result);
this.dispatcher.Invoke(() => this.StatusText = filePath, DispatcherPriority.Render);
}
}
this.RaisePropertyChanged("Images");
}));
};
var thread = new Thread(threadStart);
thread.IsBackground = true;
thread.Start();
}
第二个选项:
// on button click:
this.CreateImageList();
private async void CreateImageList()
{
await this.CreateImageListAsync();
}
private async Task CreateImageListAsync()
{
this.images.Clear();
var countTotal = this.fileBuffer.Count();
var index = 0;
await Task.Run(() => this.dispatcher.Invoke(
(() =>
{
foreach (var filePath in this.fileBuffer)
{
var result = new ImageObject(filePath);
if (result.Image != null)
{
this.images.Add(result);
var percent = (index * 100) / countTotal;
this.DoForce(() => this.StatusText = percent + "% " + filePath);
}
index++;
}
this.RaisePropertyChanged("Images");
}), DispatcherPriority.SystemIdle));
}
public void DoForce(Action action)
{
this.dispatcher.Invoke(DispatcherPriority.Render, action);
}
在第一个和第二个选项中,程序确实有效。但是在第一个和第二个选项中,用户无法访问 UI
【问题讨论】:
-
您遇到什么错误或“无法到达”实际上是如何显示的?请提供准确的故障观察结果,而不是对您所看到的内容的一些解释!
-
这是因为您将委托放在 UI 调度程序本身上,无论如何将其放回 UI 线程。您应该只在 UI 调度程序上委托 UI 任务,其余的可以在后台线程上运行。
-
没有错误!程序运行良好!但是我不能移动窗口,或者点击任何按钮等等......
-
罗希特大桶,谢谢!这肯定是解决方案
-
您不应该手动运行线程。最好使用 async/await 模式,如果您运行旧的 Visual Studio(2013 之前)版本,最好使用
Task<T>类型
标签: c# wpf multithreading mvvm dispatcher