我正在使用异步等待任务来运行此代码,我想在提取时更改进度条
public async Task<string> DownloadAndExtractFile(string source, string destination, string ItemDownload) //source = File Location //destination = Restore Location
{
string zPath = @"C:\Program Files\7-Zip\7zG.exe";
ProcessStartInfo pro = new ProcessStartInfo();
pro.WindowStyle = ProcessWindowStyle.Hidden;
pro.FileName = zPath;
pro.Arguments = "x \"" + source + "\" -o" + destination;
await Task.Run(() =>
{
Restore.frmRestore.progressBar1.Value = 50; //already set to public
try
{
Process x = Process.Start(pro);
Task.WaitAll();
Restore.frmRestore.progressBar1.Value = 100;//already set to public
x.Close();
Console.WriteLine("Extract Successful.");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
);
return "Success";
}
如何在任务运行时更改进度条值。这是错误“跨线程操作无效:控制'progressBar1'从除创建它之外的线程访问。”
答案 0 :(得分:3)
Use the Progress<T>
type to report progress,正如我在博客中描述的那样:
public async Task<string> DownloadAndExtractFile(string source, string destination, string ItemDownload)
{
string zPath = @"C:\Program Files\7-Zip\7zG.exe";
ProcessStartInfo pro = new ProcessStartInfo();
pro.WindowStyle = ProcessWindowStyle.Hidden;
pro.FileName = zPath;
pro.Arguments = "x \"" + source + "\" -o" + destination;
IProgress<int> progress = new Progress<int>(
value => { Restore.frmRestore.progressBar1.Value = value; });
await Task.Run(() =>
{
progress.Report(50);
try
{
Process x = Process.Start(pro);
Task.WaitAll();
progress.Report(100);
x.Close();
Console.WriteLine("Extract Successful.");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
});
return "Success";
}
答案 1 :(得分:0)
您可以使用Progress<T>
。这允许您通知UI线程您的进度并更新进度条。
所以,在调用该任务的地方,执行此操作:
Progress<int> progress = new Progress<int>(i => Restore.frmRestore.progressBar1.Value = i);
await DownloadAndExtractFile(source, destination, ItemDownload, progress);
在您的方法中,您可以像这样使用progress
:
public async Task<string> DownloadAndExtractFile(string source, string destination, string ItemDownload, IProgress<int> progress)
{
// shortened for clarity
await Task.Run(() =>
{
progress.Report(50);
try
{
Process x = Process.Start(pro);
Task.WaitAll();
progress.Report(100);
x.Close();
Console.WriteLine("Extract Successful.");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
});
return "Success";
}
注意:您必须将progress
作为接口类型IProgress<int>
传递才能访问该接口的Report
方法。
答案 2 :(得分:0)
直接解决方案,但不像Progress<T>
那样花哨:
创建一个函数:
private void UpdateProgress(int percent)
{
if (Restore.frmRestore.progressBar1.InvokeRequired)
{
UpdateProgress.Invoke(percent);
}
else
{
Restore.frmRestore.progressBar1.Value = percent;
}
}
然后调用它而不是直接设置值。
澄清:.Invoke
确实在主线程(即UI-Thread)上执行该函数。
这是1:1你想要的但是我会使用Progress<T>