Show download window

My application in WPF is loading external resources, so I want to show the loading form during program loading.

I tried to create a form and show in front of the download code and close when the download was finished.

private void Window_Loaded(object sender, RoutedEventArgs e) { LoadForm lf = new LoadForm(); lf.Visibility = Visibility.Visible; // Al code that delays application loading lf.Close(); } 

But the only thing I get is that the form is displayed when the download is complete and closes immediately.

I think I need to use System.Threading, but not sure.

Thanks.

Note I load all external application resources into the Window_Loaded () method, and not into the main class method.

+4
source share
3 answers

You should look at this MSDN article about creating SplashScreen in WPF. Essentially, you add the image that you want to show in your project, and set the "Build Action on SplashScreen", which will be displayed when your program starts and disappears when the main application window is displayed.


You can also try to import the System.ComponentModel class and use BackgroundWorker to show your loading form, this will allow you to maintain the responsiveness of your user interface.

 public partial class MainWindow : Window { Window1 splash; BackgroundWorker bg; public MainWindow() { InitializeComponent(); bg = new BackgroundWorker(); bg.DoWork += new DoWorkEventHandler(bg_DoWork); bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted); } void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { splash.Hide(); } void bg_DoWork(object sender, DoWorkEventArgs e) { System.Threading.Thread.Sleep(10000); } private void Window_Loaded(object sender, RoutedEventArgs e) { splash = new Window1(); splash.Show(); bg.RunWorkerAsync(); } } 
+5
source

You must put your multi-user code in a background thread (for this you can use BackgroundWorker, Task or Async Await, depending on the version of your network)

 private void Window_Loaded(object sender, RoutedEventArgs e) { LoadForm lf = new LoadForm(); lf.Visibility = Visibility.Visible; //start the time consuming task in another thread } HeavyTaskCompleteEvent() { lf.Close(); } 

Also pay attention to the best way to display the loading screen. You can also show the animation in the main window. I don’t think showing the form is the best way.

+2
source

I recently created a Loader class that you could use. It shows a window when executing your boot method, closes it when finished, and gives you the result of the method:

 public class Loader<TActionResult>:FrameworkElement { private Func<TActionResult> _execute; public TActionResult Result { get; private set; } public delegate void OnJobFinished(object sender, TActionResult result); public event OnJobFinished JobFinished; public Loader(Func<TActionResult> execute) { if (execute == null) throw new ArgumentNullException("execute"); _execute = execute; } private Window GetWaitWindow() { Window waitWindow = new Window { Height = 100, Width = 200, WindowStartupLocation = WindowStartupLocation.CenterScreen, WindowStyle = WindowStyle.None }; waitWindow.Content = new TextBlock { Text = "Please Wait", FontSize = 30, FontWeight = FontWeights.Bold, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center }; return waitWindow; } public void Load(Window waitWindow = null) { if (waitWindow == null) { waitWindow = GetWaitWindow(); } BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += delegate { Dispatcher.BeginInvoke(new Action(delegate { waitWindow.ShowDialog(); })); Result = _execute(); Dispatcher.BeginInvoke(new Action(delegate() { waitWindow.Close(); })); }; worker.RunWorkerCompleted += delegate { worker.Dispose(); if (JobFinished != null) { JobFinished(this, Result); } }; worker.RunWorkerAsync(); } } 

How to use it:

 Loader<TResult> loader = new Loader<TResult>(MethodName); loader.JobFinished += new Loader<TResult>.OnJobFinished(loader_JobFinished); loader.Load(); void loader_JobFinished(object sender, TResult result) { // do whatever you want with your result here } 
+1
source

All Articles