Reputation: 161
Im trying to make active a progress ring when executing a command on cmd, button stay pressed when command is executing and released when the process get done but i can't get the progress ring activated when process is still running, seems like WaitForExit is not working for me.
private void Button_Click(object sender, RoutedEventArgs e)
{
if (ComboBox.Text == "")
{
MessageBox.Show("Select a drive");
}
else
{
try
{
ProgressRing.IsActive=true;
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine("attrib -r -s -h " + ComboBox.Text + "*.* /s /d");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
ProgressRing.IsActive=false;
}
catch (Exception i)
{
Console.WriteLine("{0} Exception caught.", i);
MessageBox.Show("Error");
}
}
}
Upvotes: 0
Views: 1529
Reputation: 169220
You should call the blocking WaitForExit
method on a background thread. The UI thread cannot both display your ProgressRing
and wait for the process to finish simultaneously. Try this:
private void Button_Click(object sender, RoutedEventArgs e)
{
if (ComboBox.Text == "")
{
MessageBox.Show("Select a drive");
}
else
{
ProgressRing.IsActive = true;
string text = ComboBox.Text;
Task.Factory.StartNew(() =>
{
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine("attrib -r -s -h " + text + "*.* /s /d");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
}).ContinueWith(task =>
{
ProgressRing.IsActive = false;
if (task.IsFaulted)
{
Console.WriteLine("{0} Exception caught.", task.Exception);
MessageBox.Show("Error");
}
}, System.Threading.CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
}
}
Upvotes: 2