views:

3114

answers:

3

I need to show a screen or something, saying 'Loading' or whatever while long process are working.

I am creating a application with the Windows Media Encoder SDK and it takes awhile to initialize the encoder. I would like for a screen to pop up saying 'Loading' while it is starting the encoder, and then for it to disappear when the encoder is done and they can continue with the application.

Any help would be appreciated. Thanks!

A: 

There are many ways you could do this. The most simple might be to show a modal dialog, then kick off the other process, once it is completed you an then close the displayed dialog. You will need to handle the display of the standard X to close though. However, doing that all in the standard UI thread would lock the UI until the operation is complete.

Another option might be to have a "loading" screen that fills your default form, bring it to the front, then trigger the long running process on a secondary thread, once it is completed you can notify the UI thread and remove the loading screen.

Those are just a few ideas, and it really depends on what you are doing.

Mitchel Sellers
@Mitchel I tried showing a form, and after the form was Shown have it kick off my code to initialize the encoder... Only problem was that it would not load my label that says Loading, until after the encoder was initialized.
Bruno43
@Bruno - that is due to the UI thread blocking as I mentioned. A call to Application.DoEvents() should get around that. Otherwise, the multi-threaded approach using Background Worker is your best bet.
Mitchel Sellers
A: 

Two things you can try.

After setting your label (as mentioned in the comment to Mitchel) call Application.DoEvents()

Another option you have is to run the initialization code for the encoder in a BackgroundWorker process.

Tom Anderson
Any good examples on Background Worker Processes? I am googling now though ;)
Bruno43
It is a standard component, see OwenP's reply for good example.
Tom Anderson
+4  A: 

Create a Form that will serve as the "Loading" dialog. When you're ready to initialize the encoder, display this form using the ShowDialog() method. This causes it to stop the user from interacting with the form that is showing the loading dialog.

The loading dialog should be coded in such a way that when it loads, it uses a BackgroundWorker to initialize the encoder on a separate thread. This ensures the loading dialog will remain responsive. Here's an example of what the dialog form might look like:

Imports System.ComponentModel

Public Class LoadingForm ' Inherits Form from the designer.vb file

    Private _worker As BackgroundWorker

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)

        _worker = New BackgroundWorker()
        AddHandler _worker.DoWork, AddressOf WorkerDoWork
        AddHandler _worker.RunWorkerCompleted, AddressOf WorkerCompleted

        _worker.RunWorkerAsync()
    End Sub

    ' This is executed on a worker thread and will not make the dialog unresponsive.  If you want
    ' to interact with the dialog (like changing a progress bar or label), you need to use the
    ' worker's ReportProgress() method (see documentation for details)
    Private Sub WorkerDoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
        ' Initialize encoder here
    End Sub

    ' This is executed on the UI thread after the work is complete.  It's a good place to either
    ' close the dialog or indicate that the initialization is complete.  It's safe to work with
    ' controls from this event.
    Private Sub WorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

End Class

And, when you're ready to display the dialog, you would do so like this:

Dim frm As New LoadingForm()
frm.ShowDialog()

There are more elegant implementations and better practices to follow, but this is the simplest.

OwenP
Exactly what I was looking for.@Mitchel @Tom AndersonThanks for the help! Everyones answer truly helped me!
Bruno43