Thursday, 3 December 2009

VB DotNet - Please Wait window

We have need of a "Please Wait" type window in our code that comes up and displays only for as long as a particular chunk of code is running. Then it terminates.

I've done this by creating a form with the required "please wait" text and a nice animated logo which I load up in a new thread when I want it to show. It will thus display even though the code is busy doing something else.

Don't forget to thread.abort when it's finished to clear the form, or keep a global variable somewhere that the thread looks at to know when to stop itself. Alternatively, if you're feeling really brave you could use inter-thread communications, although it seems OTT for something as simple as this. Also, you'll probably want to set the form to "Topmost" so it's always on top.

Interestingly, threads seem to be a good way of opening up new forms when old forms are still open and you want them both to be functional even if the other one is busy. For example:


Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim EBT As New Threading.Thread(AddressOf ExistingBookingsThread)
EBT.Start()
End Sub

Private Sub ExistingBookingsThread()
Dim EB As New ExistingBookings
EB.ShowDialog()
End Sub

This code snippet handles a button press for a new form by starting a new thread which calls "ExistingBookingsThread" which is a holding subroutine that displays the form then terminates when the form is closed.

Nick

No comments:

Post a Comment