代码之家  ›  专栏  ›  技术社区  ›  pixeldev

在vb.net中显示加载屏幕

  •  3
  • pixeldev  · 技术社区  · 17 年前

    我需要显示一个屏幕或什么的,说“加载”或什么的,而漫长的过程正在工作。

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

    3 回复  |  直到 17 年前
        1
  •  10
  •   OwenP    17 年前

    创建一个用作“加载”对话框的表单。当您准备初始化编码器时,使用 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()
    

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

        2
  •  0
  •   Mitchel Sellers    17 年前

    有很多方法可以做到这一点。最简单的方法可能是显示一个模式对话框,然后启动另一个进程,完成后关闭显示的对话框。不过,您需要处理标准X的显示才能关闭。但是,在标准UI线程中执行这些操作将锁定UI,直到操作完成。

    另一种选择可能是有一个“加载”屏幕来填充默认表单,将其置于前端,然后在辅助线程上触发长时间运行的进程,一旦完成,您可以通知UI线程并删除加载屏幕。

    这些只是一些想法,这取决于你在做什么。

        3
  •  0
  •   Siddharth Rout    14 年前

    设置标签后(如对Mitchel的评论中所述)调用 Application.DoEvents()

    另一个选项是在BackgroundWorker进程中运行编码器的初始化代码。