vb.net - 在 vb.net 中显示加载屏幕

标签 vb.net visual-studio visual-studio-2005

我需要显示一个屏幕或其他东西,在长时间的过程中说“正在加载”或其他任何东西。

我正在使用 Windows Media Encoder SDK 创建一个应用程序,初始化编码器需要一些时间。我希望在启动编码器时弹出一个屏幕说“正在加载”,然后在编码器完成后它消失,他们可以继续使用应用程序。

任何帮助,将不胜感激。谢谢!

最佳答案

创建一个用作“加载”对话框的表单。当您准备好初始化编码器时,请使用 ShowDialog() 显示此表单。方法。这会导致它阻止用户与显示加载对话框的表单交互。

加载对话框的编码方式应该是在加载时使用 BackgroundWorker在单独的线程上初始化编码器。这确保加载对话框将保持响应。下面是对话框形式的示例:

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

而且,当您准备好显示对话框时,您可以这样做:
Dim frm As New LoadingForm()
frm.ShowDialog()

有更优雅的实现和更好的实践可以遵循,但这是最简单的。

关于vb.net - 在 vb.net 中显示加载屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/403202/

相关文章:

c - 是否有计算 printf 格式字符串的参数数量的函数?

visual-studio-2005 - 为什么 Visual Studio 2005 不会生成 Xml 序列化程序集?

asp.net - 错误 : EntitySet 'Building' that was specified in page markup does not exist on the Container

c# - WinForms-何时调用Dispose?什么时候隐式?

vb.net - 这个简单的 VB.Net 类线程安全吗?如果没有,我该如何改进?

没有 UI 的 VB.net 程序

visual-studio - 将项目移动到不同的 Team Foundation Server

wpf - Visual Studio 2015 中 WPF Windows 顶部的东西是什么

c# - 像 Windows 窗体一样使用 .designer.cs 生成代码

c# - 部署 Office 2007 加载项