What is a splash screen?
“A splash screen is a graphical
control element consisting of window containing
an image, a logo and the current version
of the software. A splash screen usually appears while a game or program is
launching.” According to Wikipedia J
That says everything there is
to say. There’s nothing more to add I think.
More specifically, it's
something like we see below:
What has that to do with us?
Well, you certainly had spent
your time in scripting and writing tons of code in powershell and you found it
funny. But at a certain point, you probably realize that the longer your script
is, the more time it takes to load.
And before your computer, when
you load your script, you’re like:
To sum up, if your application
is too big, it will take time before your entire GUI is loaded and and you don't really want your user to wonder "hey! what's going on?". That’s where
splash screen plays its role.
The idea of a splash screen
came to me when I was asked to create a tool that wrapped ImageX for creating a
WIM image. I realized that my tool took too much time to load and I needed something
to keep the user busy.
How do we do it in Powershell?
The tips is: runspace
We are just going to
use a runspace to load a second form before loading your code and close it when
it’s finished. That is the big idea behind. You may be saying: what
the heck is he talking about?
I only have a shallow
knowledge about runspace too but, all I can say is that it’s really helpful
when you want to do multi-threading (to have many processes working at the same
time - in noobs way of speaking J).
In my previous post how
to apply a theme in Powershell, I introduced Mahapps which uses a metro theme.
We are going to use one of the component included in that theme for creating
the splash screen. I think you’ve guessed it already: it’s for the progressbar in the
splash screen.
Let’s move on to the
code.
How to use the splash
screen?
You just have to copy
and paste the following code in your main ps1 file at the top. This way the
runspace which contains the second form will be loaded before everything and
can be called at any time inside the script.
If you want to add your logo and text, just edit:
$hash.Logo.Source=".\form\resources\powershel64x64.png"
$hash.LoadingLabel.Content= "Your Code is loading"
As you can see, there are
two functions:
- Start-splashScreen
- Close-splashScreen
Those functions are
used to open and close the runspace.
You just have to put Start-splashScreen before the script
block
Start-SplashScreen
try {
########################
# your incredible code
########################
}
catch{
$ErrorMessage =
$_.Exception.Message
Write-Host $ErrorMessage
}
It’s better to use a
try and catch if your code potentially generates error. So the splash screen
will be closed in any case at the end.
And at the end of your
file, before you call ShowDialog() on your form, or when the code you wanted to be executed
is finished, close the splash screen.
close-SplashScreen
$Form.ShowDialog() | Out-Null
Of course, as you could
understand, you can use a splash screen for many purposes. Let your imagination
take you away.
Here is a little preview:
You can download all files here.
And that’s it for this
third part, hope you enjoyed it. J And see you in the next part