我需要显示一个屏幕或其他内容,在长时间过程正常工作时说"正在加载"或其他任何内容.
我正在使用Windows Media Encoder SDK创建应用程序,初始化编码器需要一些时间.我希望屏幕在启动编码器时弹出说"正在加载",然后在编码器完成时它会消失并且可以继续使用应用程序.
任何帮助,将不胜感激.谢谢!
创建一个将用作"加载"对话框的表单.准备好初始化编码器时,使用该ShowDialog()
方法显示此表单.这会导致它阻止用户与显示加载对话框的表单进行交互.
加载对话框应该以这样的方式编码:加载时,它使用a 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()
有更多优雅的实现和更好的实践可以遵循,但这是最简单的.