Reputation: 3689
i'd like to create loading screen during executing funcion - something like loading screen in games, new window or some graphics.
So I press button "Start" - app starts doing some stuff, and childform window shows with graphic -"Function in progress, please wait"
How to do it?
Upvotes: 2
Views: 9387
Reputation: 6903
This is very simple. You need to display this form by ShowDialog instead of Show. And execute 'some stuff' in another thread which after invoking Hide. That is the first come to my mind:
void DoSomeStuff ()
{
//Showing splash screen
SplashForm.ShowDialog(this);
new Thread (()=>
{
//do stuff what you need
//Hiding splash screen
SplashForm.Invoke (new Action (SplashForm.Hide));
}).Start();
}
By the way, there are over 9000 ways to do this...)
Upvotes: 2
Reputation: 47560
You can use a BackgroundWorker in order to execute your functionality in a separate thread. BackgroundWorker has a lot of feature to support for the functionality that you are looking for. If you didn't use another thread to execute your function, the main thread (UI thread) will be busy and child window won't work properly.
Some key notes on backgroundWorker. DoWork in a different thread, report progress to the main thread and cancel the asynchronous process are the most important functionalities in BackgroundWorker. Below example demonstrates those three functionalities quite clearly. But it displays the actual percentage of the task completion. If you just want to show a message and ProgressBar with Marquee style, you don't have to switch on the WorkerReportsProgress
flag and no need ProgressChanged
event implementation.
There are heaps of examples available on the web.
using System;
using System.Threading;
using System.ComponentModel;
class Program
{
static BackgroundWorker _bw;
static void Main()
{
_bw = new BackgroundWorker
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
_bw.DoWork += bw_DoWork;
_bw.ProgressChanged += bw_ProgressChanged;
_bw.RunWorkerCompleted += bw_RunWorkerCompleted;
_bw.RunWorkerAsync ("Hello to worker");
Console.WriteLine ("Press Enter in the next 5 seconds to cancel");
Console.ReadLine();
if (_bw.IsBusy) _bw.CancelAsync();
Console.ReadLine();
}
static void bw_DoWork (object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 100; i += 20)
{
if (_bw.CancellationPending) { e.Cancel = true; return; }
_bw.ReportProgress (i);
Thread.Sleep (1000); // Just for the demo... don't go sleeping
} // for real in pooled threads!
e.Result = 123; // This gets passed to RunWorkerCompleted
}
static void bw_RunWorkerCompleted (object sender,
RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
Console.WriteLine ("You canceled!");
else if (e.Error != null)
Console.WriteLine ("Worker exception: " + e.Error.ToString());
else
Console.WriteLine ("Complete: " + e.Result); // from DoWork
}
static void bw_ProgressChanged (object sender,
ProgressChangedEventArgs e)
{
Console.WriteLine ("Reached " + e.ProgressPercentage + "%");
}
}
Upvotes: 6
Reputation: 3826
The way that games do this is they make two different applications. The one is not really a loading screen but more of a launcher that checks for updates, shows news and launches the game. The second application is the game it's self. If you do this in C# the launcher can launch the game by calling:
System.Diagnostics.Process.Start("[APPLICATION_PATH]");
Upvotes: 3