I a WPF application I have the following event-handler of a button-click in a code-behind file of a window:
private void AddNewRecBtn_Click(object sender, RoutedEventArgs e)
{
DimRec.Visibility = System.Windows.Visibility.Visible;
PrBarLayer.Visibility = System.Windows.Visibility.Visible;
try
{
WriteAndClose();
}
catch (Exception)
{
throw;
}
}
These lines of code:
DimRec.Visibility = System.Windows.Visibility.Visible;
PrBarLayer.Visibility = System.Windows.Visibility.Visible;
just make visible a rectangle DimRec
with Opacity="0.5" Fill="Gray"
for dimming the window during showing ProgressBar and a StackPanel PrBarLayer
with a ProgressBar itself.
WriteAndClose();
method performs lots of stuff, including LINQ to SQL query and heavy visualization on the screen, which takes several seconds (while it's going on, there is nothig happening on the screen, everything seems freezed).
During this period of several seconds I'd like to show user a dimmed window control and a progress bar animated, but the problem is that these layers, which code make visible, in fact don't appear. If I remove WriteAndClose();
method from this handler, everything is OK - ProgressBar gets visible.
It seems that WPF doesn't render visually controls at once and then resources are taken to the next method WriteAndClose
, everything freezes and then screen get rendered when all is done already and Visibility of layer of progress bar is made Collapsed.
What could be the solution? How I could make ProgressBar show first and only then do remaining heavy stuff?