0

I have to do a lot of operations when I start my application. I have to show a splash screen with loaded percentage. The idea is the first time to record the time necessary to load, then serialize it with other data. The following times I have all what's necessary to show the percentage on the splash screen.

At the beginning therefore I create a splashscreen window with some info

var bmpSplash = new BitmapImage(new Uri(@"pack://application:,,,/Resources/Images/splashScreen.png", UriKind.RelativeOrAbsolute));
splashScreen = new EmptyWindow.EmptyWindow(bmpSplash) { SizeToContent = SizeToContent.Manual, Width = 612, Height = 378, Background = Brushes.Transparent, WindowStyle = WindowStyle.None, AllowsTransparency = true, Topmost = true };
...
var tbPercetage = new TextBlock() { Text="AAAA", FontSize = 16, Width = splashScreen.Width, TextAlignment = TextAlignment.Left, Height = 50, VerticalAlignment = VerticalAlignment.Bottom, Margin = new Thickness(150, 0, 0, 50) };
...
splashScreen.grdMain.Children.Add(otbVersion4);
splashScreen.Show();

then I create a Dispatcher timer which has to update the percentage:

DispatcherTimer tmrSplashPercentage = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(125) };
tmrSplashPercentage.Tick += (sender2, args) =>
{
((TextBlock)splashScreen.grdMain.Children[3]).Dispatcher.Invoke(() => ((TextBlock)splashScreen.grdMain.Children[3]).Text= DateTime.Now.Ticks.ToString(), DispatcherPriority.Background);
};<---- it is the 4th children

Dispatcher.CurrentDispatcher.Invoke(new Action(() => { tmrSplashPercentage.Start(); }));

I use the Invoke because I have seen that by doing this I can force update. But if I put a breakpoint in the timer tick I have seen that it is not fired until all the load procedure is done. So either it doesn't start or the tick event is not fired.

Thank you for any help! Patrick

---ADD---

Sorry forgot to say that the events take place in the MainView() and then continues in the WindowViewBase_Loaded event.

I know that I might now use a timer but put several updates after each step but that wouldn't make it precise....

Patrick
  • 3,073
  • 2
  • 22
  • 60
  • I'd venture to say that your whole loading procedure is a blocking call, which prevents the handling of events until it is done. But it's hard to tell from this. – Jens Nov 27 '16 at 15:17
  • It might be but shouldn't the Dispatcher timer live on its own timing? This is what timers are for... Should it be as you say, is there any workaround? – Patrick Nov 27 '16 at 15:23
  • 1
    The `DispatcherTimer` runs on the UI thread. If you block the UI thread with your loading procedure, it will not execute. The solution is to move your loading procedure to a background thread. – Nico Schertler Nov 27 '16 at 15:27
  • I might do that but I'm a little afraid about it. No other solution??? What if I used a different timer? the not dispatcher one? Or changing priority?? thanks – Patrick Nov 27 '16 at 15:32
  • Not really into WPF, so I don't know if you could do the same as with Winforms, and just manually call Refresh() on the form from inside your (blocking) loading procedure at certain steps. But it is really quite too broad to answer I would say, as it depends a lot on what your loading steps look like. Best bet is still to load in a background thread. – Jens Nov 27 '16 at 15:43
  • Ok I have experimentated that you guys are right. So thanks. But thinking about an alternative solution could I use this strategy? – Patrick Nov 27 '16 at 16:35
  • Main program MainView--->splashScreen created and launched with var ss = new splashScreen(20) where 20 seconds is the splashScreen duration. it is the splash screen itself that updates the percentage through a timer. The timer lives in the splashscreen and not in the mainprogram – Patrick Nov 27 '16 at 16:41

0 Answers0