i am using async await task to run this code and i want to change the progress bar when extracting
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";
}
how to change the progressbar value when task running. this is the error "Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on."
Use the
Progress<T>
type to report progress, as I describe on my blog:You can use a
Progress<T>
. That allows you to inform the UI thread of your progress and update your progress bar.So, at the place where call that task, do this:
And in your method you can use this
progress
like this:Note: that you have to pass
progress
as interface typeIProgress<int>
to be able to access theReport
method of that interface.Direct solution, but not as fancy as
Progress<T>
:Create a function:
And then call it instead of setting the value directly.
To clarify:
.Invoke
does execute the function on the main thread (which is the UI-Thread.).This does 1:1 what you wan't but I would use
Progress<T>