Application Dispatcher 不更新也不报错
Application Dispatcher doesn't update and doesn't give errors
我有一个应用程序,我需要下载很多文件。实际上下载我使用这个代码:
System.Net.ServicePointManager.DefaultConnectionLimit = Int32.Parse(Properties.Settings.Default["download_threads"].ToString());
var block = new System.Threading.Tasks.Dataflow.ActionBlock<string>(async url =>
{
await DownloadLibraries(url);
}, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = Int32.Parse(Properties.Settings.Default["download_threads"].ToString()) });
foreach (var url in urls)
{
block.Post(url);
}
block.Complete();
await block.Completion;
它工作得很好,它通过并行下载下载了我需要的一切。
在 DownloadLibraries 方法中,我想更新一个标签。
这是代码:
Application.Current.Dispatcher.Invoke(new Action(() =>
{
string urlnum = this.filesToDownload.Content.ToString().Split('/')[0];
if (Array.IndexOf(urls, url) > Int32.Parse(urlnum))
this.filesToDownload.Content = Array.IndexOf(urls, url) + "/" + count.ToString();
if ((Array.IndexOf(urls, url) * 100) > totalProgress.Value)
this.totalProgress.Value = (Array.IndexOf(urls, url) * 100) / count;
}));
它没有给我任何错误,但它也没有更新标签。
如果我做 Console.Write((Array.IndexOf(urls, url) * 100) / count);它工作正常,我可以在 visual studio 中的输出 space 中看到百分比上升,但标签根本没有更新。可能是什么问题?
您应该在元素的 Dispatcher
:
上调用 .Invoke()
this.filesToDownload.Dispatcher.Invoke(new Action(() =>
{
string urlnum = this.filesToDownload.Content.ToString().Split('/')[0];
if (Array.IndexOf(urls, url) > Int32.Parse(urlnum))
this.filesToDownload.Content = Array.IndexOf(urls, url) + "/" + count.ToString();
if ((Array.IndexOf(urls, url) * 100) > totalProgress.Value)
this.totalProgress.Value = (Array.IndexOf(urls, url) * 100) / count;
}));
我有一个应用程序,我需要下载很多文件。实际上下载我使用这个代码:
System.Net.ServicePointManager.DefaultConnectionLimit = Int32.Parse(Properties.Settings.Default["download_threads"].ToString());
var block = new System.Threading.Tasks.Dataflow.ActionBlock<string>(async url =>
{
await DownloadLibraries(url);
}, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = Int32.Parse(Properties.Settings.Default["download_threads"].ToString()) });
foreach (var url in urls)
{
block.Post(url);
}
block.Complete();
await block.Completion;
它工作得很好,它通过并行下载下载了我需要的一切。 在 DownloadLibraries 方法中,我想更新一个标签。 这是代码:
Application.Current.Dispatcher.Invoke(new Action(() =>
{
string urlnum = this.filesToDownload.Content.ToString().Split('/')[0];
if (Array.IndexOf(urls, url) > Int32.Parse(urlnum))
this.filesToDownload.Content = Array.IndexOf(urls, url) + "/" + count.ToString();
if ((Array.IndexOf(urls, url) * 100) > totalProgress.Value)
this.totalProgress.Value = (Array.IndexOf(urls, url) * 100) / count;
}));
它没有给我任何错误,但它也没有更新标签。 如果我做 Console.Write((Array.IndexOf(urls, url) * 100) / count);它工作正常,我可以在 visual studio 中的输出 space 中看到百分比上升,但标签根本没有更新。可能是什么问题?
您应该在元素的 Dispatcher
:
.Invoke()
this.filesToDownload.Dispatcher.Invoke(new Action(() =>
{
string urlnum = this.filesToDownload.Content.ToString().Split('/')[0];
if (Array.IndexOf(urls, url) > Int32.Parse(urlnum))
this.filesToDownload.Content = Array.IndexOf(urls, url) + "/" + count.ToString();
if ((Array.IndexOf(urls, url) * 100) > totalProgress.Value)
this.totalProgress.Value = (Array.IndexOf(urls, url) * 100) / count;
}));