Show a loading screen in vb.net

前端 未结 3 1263
独厮守ぢ
独厮守ぢ 2021-02-10 14:58

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

相关标签:
3条回答
  • 2021-02-10 15:38

    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.

    0 讨论(0)
  • 2021-02-10 15:42

    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.

    0 讨论(0)
  • 2021-02-10 15:49

    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.

    0 讨论(0)
提交回复
热议问题