在vb.net中显示加载屏幕 [英] Show a loading screen in vb.net

查看:177
本文介绍了在vb.net中显示加载屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示一个屏幕或类似的东西,在长时间的工作过程中说正在加载"或其他内容.

I need to show a screen or something, saying 'Loading' or whatever while long process are working.

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

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.

任何帮助将不胜感激.谢谢!

Any help would be appreciated. Thanks!

推荐答案

创建一个将用作正在加载"对话框的表单.准备初始化编码器时,请使用ShowDialog()方法显示此表单.这将导致它阻止用户与显示加载对话框的表单进行交互.

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.

加载对话框的编码方式应使其在加载时使用BackgroundWorker在单独的线程上初始化编码器.这样可以确保加载对话框保持响应状态.这是对话框形式的示例:

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.

这篇关于在vb.net中显示加载屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆