开发者

Strange behaviour of code in WPF (rendering issue)

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, 开发者_StackOverflowRoutedEventArgs 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?


You will have to move the executing of WriteAndClose to a different thread.

However, then you will only be able to manipulate the UI using [Control.]Dispatcher.BeginInvoke.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜