在 vb.net 中显示加载屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/403202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show a loading screen in vb.net
提问by pixeldev
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 it takes awhile to initialize the encoder. I would like for a screen to pop up saying 'Loading' while it is starting the encoder, and then for it to disappear when the encoder is done and they can continue with the application.
我正在使用 Windows Media Encoder SDK 创建一个应用程序,初始化编码器需要一些时间。我希望在启动编码器时弹出一个屏幕说“正在加载”,然后在编码器完成时它消失,他们可以继续使用应用程序。
Any help would be appreciated. Thanks!
任何帮助,将不胜感激。谢谢!
回答by OwenP
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.
创建一个用作“加载”对话框的表单。当您准备好初始化编码器时,请使用ShowDialog()
方法显示此表单。这会导致它阻止用户与显示加载对话框的表单交互。
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:
加载对话框应该以这样的方式编码,当它加载时,它使用 aBackgroundWorker
来初始化单独线程上的编码器。这确保加载对话框将保持响应。下面是对话框形式的示例:
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.
有更优雅的实现和更好的实践可以遵循,但这是最简单的。
回答by Mitchel Sellers
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.
有很多方法可以做到这一点。最简单的可能是显示一个模态对话框,然后启动另一个进程,一旦完成,您就关闭显示的对话框。不过,您需要处理标准 X 的显示才能关闭。但是,在标准 UI 线程中执行所有操作会锁定 UI,直到操作完成。
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.
另一种选择可能是有一个“加载”屏幕来填充您的默认表单,将其放在最前面,然后在辅助线程上触发长时间运行的进程,一旦完成,您可以通知 UI 线程并删除加载屏幕。
Those are just a few ideas, and it really depends on what you are doing.
这些只是一些想法,这实际上取决于您在做什么。
回答by Tom Anderson
Two things you can try.
你可以尝试两件事。
After setting your label (as mentioned in the comment to Mitchel) call Application.DoEvents()
设置标签后(如对 Mitchel 的评论中所述)调用 Application.DoEvents()
Another option you have is to run the initialization code for the encoder in a BackgroundWorker process.
另一个选择是在 BackgroundWorker 进程中运行编码器的初始化代码。