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 i
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.