Reputation: 757
I created a border and a label in a user control. When the window loads, it reads a list of tasks to be performed from a text file and then creates the same number of user controls and adds it to stack panel.
<UserControl x:Class="DiagnoseTool.TaskControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="515" Height="40">
<UserControl.Resources>
<Storyboard x:Key="ShowBlink">
<ColorAnimation From="White" To="LightGreen" Duration="0:0:0.5" Storyboard.TargetName="StatusRectangle" Storyboard.TargetProperty="Background.Color" AutoReverse="True" RepeatBehavior="Forever"/>
<!--ColorAnimation From="LightGreen" To="White" Duration="0:0:1" Storyboard.TargetName="StatusRectangle" Storyboard.TargetProperty="Background.Color"/-->
</Storyboard>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.08*"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border x:Name="StatusRectangle" Margin="5" Grid.Column="0" Background="White" Width="20" Height="20" BorderThickness="1" BorderBrush="Black"></Border>
<Label x:Name="TaskNameLabel" Grid.Column="1" Margin="5" FontSize="12" Content="" FontFamily="Calibri"></Label>
</Grid>
Border has a animation which changes the background color from white to green when the task is being executed. Code behind of taskcontrol contains a method which is:
/// <summary>
/// This function is used to perform some task
/// </summary>
/// <returns></returns>
public bool StartShowingProgress()
{
bool bRet = true;
try
{
sbdStatusDisplay = (Storyboard)FindResource("ShowBlink");
sbdStatusDisplay.Begin(this, true);
}
catch (Exception ex)
{
bRet = false;
}
return bRet;
}
The main window loaded event handler is:
private void MainWindowLoaded(object sender, RoutedEventArgs e)
{
for(int i=0;i<taskList.Count;i++)
{
Logger.WriteToLog(taskList[i].TASKNAME);
TaskControl objTaskControl = new TaskControl(taskList[i].TASKNAME);
TasksPanel.Children.Add(objTaskControl);
}
for (int i = 0; i < taskList.Count; i++)
{
(TasksPanel.Children[i] as TaskControl).StartShowingProgress();
Thread th = new Thread(doSomeWork);
th.Start(i);
resetEvent.WaitOne();
}
}
The doSomeWork method code is:
private void doSomeWork(object val)
{
int index = (int)val;
Utilities.StartProcess(taskList[index].TASKFILE, taskList[index].TASKARGUMENT, taskList[index].CHECKRETURNVALUE);
resetEvent.Set();
}
The StartProcess code is:
Process process = new Process();
// Configure the process using the StartInfo properties.
process.StartInfo.FileName = fileToRun;
process.StartInfo.Arguments = arguments;
process.Start();
//Check if we need to wait for the process to exit
if (waitForExit)
{
process.WaitForExit();
}
When i just run the application without performing a task, it works fine and storyboard animation works properly. But with this code, In doSomeWork method, it launches notepad.exe and waits for it to get closed. But this time my Main UI hangs. I want the main UI to be up and storyboard animation to continue running indicating that task is still executing. Once the task is completed, I will then place a cross or tick image inside the border control indicating whether the operation was a success or failure. Why is the main UI hanging?
Upvotes: 0
Views: 73
Reputation: 9713
You should subscribe to the Exited
event if you want to keep your UI active whilst waiting for the process to finish.
process.Exited += new EventHandler(OnProcessExited);
process.Start();
And the event.
private void OnProcessExited(object sender, System.EventArgs e)
{
//TODO
}
Upvotes: 1