IProgress<T> 不会触发当前上下文中的事件
IProgress<T> does not fire events in current context
我已经成功地在我的 class UpdateManager 中实现了一个 Task
从我的网站空间下载文件。
public async Task DownloadPackageTask(IProgress<int> progress)
{
var webRequest = WebRequest.Create(new Uri("http://www.mywebspace.de/archive.zip"));
using (var webResponse = await webRequest.GetResponseAsync())
{
var buffer = new byte[1024];
FileStream fileStream = File.Create("Path"));
using (Stream input = webResponse.GetResponseStream())
{
int received = 0;
double total = 0d;
var size = GetFileSize(new Uri("...")); // Gets the content length with a request as the Stream.Length-property throws an NotSupportedException
if (size != null)
total = (long) size.Value;
int size = await input.ReadAsync(buffer, 0, buffer.Length);
while (size > 0)
{
fileStream.Write(buffer, 0, size);
received += size;
progress.Report((received/total)*100));
size = await input.ReadAsync(buffer, 0, buffer.Length);
}
}
}
}
效果很好,正在下载文件,而且如果我添加 Debug.Print((received/total)*100)
,它会输出正确的百分比,一切正常。该方法被标记为异步,以便它可以在任务中异步 awaited/wrapped。
问题出现在另一个 class UpdaterUi 中,它基本上是管理器和用户界面之间的接口,并调用这样的方法:
public void ShowUserInterface()
{
TaskEx.Run(async delegate
{
var downloadDialog = new UpdateDownloadDialog
{
LanguageName = _updateManager.LanguageCulture.Name,
PackagesCount = _updateManager.PackageConfigurations.Count()
};
_context.Post(downloadDialog.ShowModalDialog, null); // Do this with a SynchronizationContext as we are not on the UI thread.
var progressIndicator = new Progress<int>();
progressIndicator.ProgressChanged += (sender, value) =>
downloadDialog.Progress = value;
await TaskEx.Run(() => _updateManager.DownloadPackageTask(progressIndicator));
});
}
它从来没有调用那里的匿名方法,应该在进度变化时立即调用,但没有任何反应,我用断点调试它。
问题可能是 progressIndicator
不是在 UI 线程上创建的,而是在 TaskEx.Run
创建的新线程上创建的。它不会触发事件,并且 UI 不会连续更新它包含的进度条(它在上面初始化的 Progress
-属性 的 setter 中执行).
问题是我不知道该怎么做才能使其正常工作,我应该如何更改项目中的实现以使其正常工作,我对线程问题的看法是否正确?
提前致谢!
你对问题的猜测是正确的。 Progress<T>
应该在 UI 线程中创建,以便在 UI 线程中得到通知。
它的工作原理是捕获 SynchronizationContext
并发布委托以在捕获的上下文中执行。由于它是在非 UI 上下文(默认上下文或线程池上下文)中创建的,它将在线程池线程中引发 ProgressChanged
。
如果将此行 var progressIndicator = new Progress<int>();
移出 TaskEx.Run
,它应该可以工作(如果 ShowUserInterface
从 UI 线程调用)。
我看到您正在工作线程中创建 UpdateDownloadDialog
。你不应该那样做。也将其移动到 UI 线程。
我已经成功地在我的 class UpdateManager 中实现了一个 Task
从我的网站空间下载文件。
public async Task DownloadPackageTask(IProgress<int> progress)
{
var webRequest = WebRequest.Create(new Uri("http://www.mywebspace.de/archive.zip"));
using (var webResponse = await webRequest.GetResponseAsync())
{
var buffer = new byte[1024];
FileStream fileStream = File.Create("Path"));
using (Stream input = webResponse.GetResponseStream())
{
int received = 0;
double total = 0d;
var size = GetFileSize(new Uri("...")); // Gets the content length with a request as the Stream.Length-property throws an NotSupportedException
if (size != null)
total = (long) size.Value;
int size = await input.ReadAsync(buffer, 0, buffer.Length);
while (size > 0)
{
fileStream.Write(buffer, 0, size);
received += size;
progress.Report((received/total)*100));
size = await input.ReadAsync(buffer, 0, buffer.Length);
}
}
}
}
效果很好,正在下载文件,而且如果我添加 Debug.Print((received/total)*100)
,它会输出正确的百分比,一切正常。该方法被标记为异步,以便它可以在任务中异步 awaited/wrapped。
问题出现在另一个 class UpdaterUi 中,它基本上是管理器和用户界面之间的接口,并调用这样的方法:
public void ShowUserInterface()
{
TaskEx.Run(async delegate
{
var downloadDialog = new UpdateDownloadDialog
{
LanguageName = _updateManager.LanguageCulture.Name,
PackagesCount = _updateManager.PackageConfigurations.Count()
};
_context.Post(downloadDialog.ShowModalDialog, null); // Do this with a SynchronizationContext as we are not on the UI thread.
var progressIndicator = new Progress<int>();
progressIndicator.ProgressChanged += (sender, value) =>
downloadDialog.Progress = value;
await TaskEx.Run(() => _updateManager.DownloadPackageTask(progressIndicator));
});
}
它从来没有调用那里的匿名方法,应该在进度变化时立即调用,但没有任何反应,我用断点调试它。
问题可能是 progressIndicator
不是在 UI 线程上创建的,而是在 TaskEx.Run
创建的新线程上创建的。它不会触发事件,并且 UI 不会连续更新它包含的进度条(它在上面初始化的 Progress
-属性 的 setter 中执行).
问题是我不知道该怎么做才能使其正常工作,我应该如何更改项目中的实现以使其正常工作,我对线程问题的看法是否正确?
提前致谢!
你对问题的猜测是正确的。 Progress<T>
应该在 UI 线程中创建,以便在 UI 线程中得到通知。
它的工作原理是捕获 SynchronizationContext
并发布委托以在捕获的上下文中执行。由于它是在非 UI 上下文(默认上下文或线程池上下文)中创建的,它将在线程池线程中引发 ProgressChanged
。
如果将此行 var progressIndicator = new Progress<int>();
移出 TaskEx.Run
,它应该可以工作(如果 ShowUserInterface
从 UI 线程调用)。
我看到您正在工作线程中创建 UpdateDownloadDialog
。你不应该那样做。也将其移动到 UI 线程。