VB.NET 进度条后台工作者 [英] VB.NET progressbar backgroundworker

查看:26
本文介绍了VB.NET 进度条后台工作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的应用程序启动并且刚刚升级时,我正在执行本地数据库更新 (sqlite).

When my application starts, and it has just been upgraded, I am doing a local database update (sqlite).

是这样的:用户启动我的应用程序,然后我开始升级过程.在此升级过程中,我显示了一个具有连续进度条的表单.升级过程完成后,此表单将关闭,然后用户可以开始使用我的应用程序.

It is like that: The user starts my app, and then I start the upgrade process. During this upgrade process I am showing a form that has a continuous progressbar. This form closes when the upgrade process is done and the user can then start using my application.

但是由于升级过程非常密集,进度条不会动画.

But the progressbar won't animate since the upgrade process is so intensive.

在我的旧 VB6 版本中,我使用了一个 ActiveX-Exe,它有 1 个表单并显示一个进度条.这是我的后台工作者".

In my old VB6 version I used an ActiveX-Exe that has 1 form and shows a progressbar. This was my "background worker".

我不确定是否可以在 VB.NET 中使用相同的方法.

I am not sure if I can use the same approach in VB.NET.

我只看过一些例子,然后在后台工作人员中完成工作,但我没有看到任何进度条本身是后台工作人员的例子.

I have only seen examples that then do the work in the background worker, but I have not seen any examples where the progressbar itself was the background worker.

数据库升级需要阻塞,在数据库升级完成之前用户不能使用我的应用程序.这意味着只有进度条应该退出进程",而不是升级.

The database upgrade needs to be blocking, the user may NOT use my application before the database upgrade was done. This means that only the progressbar should "out of process", but not the upgrading.

非常感谢!

推荐答案

首先阅读这个:应用程序的使用.DoEvents()

因此,在阅读上述答案后,您将永远不会再使用 DoEvents,并且如果没有 DoEvents(和/或使 ProgressBar 无效,因此它的 Paint 事件将触发),进度条将不会动画,因为升级过程如此密集";

So after reading the above answer you will never using DoEvents ever again, and without the DoEvents (and/or Invalidating the ProgressBar so its Paint event will fire) the "progressbar won't animate since the upgrade process is so intensive"

因此 Cthulhu 的评论 - 您可以创建一个带有进度条的对话框,将该对话框设为模态并在后台工作人员上执行您的 db-stuff.";是最好的方法之一.

Hence Cthulhu's comment - "You can make a dialog with a progressbar, make that dialog modal and execute your db-stuff on a backgroundworker." is one of the best ways forward.

我已经翻译了我使用的 C# 实现,您应该可以直接将其放入.

I have translated a C# implementation of this that I use, you should be able to drop it straight in.

这是进度条表单:

Public Partial Class ThinkingProgressBar
    Inherits Form
    Private startTime As System.DateTime = DateTime.Now
    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub lblClose_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs)
        Me.Tag = "Cancelled"
        Me.Hide()
    End Sub

    Public Sub SetThinkingBar(ByVal switchedOn As Boolean)
        If switchedOn Then
            lblTime.Text = "0:00:00"
            startTime = DateTime.Now
            Timer1.Enabled = True
            Timer1.Start()
        Else
            Timer1.Enabled = False
            Timer1.Stop()
        End If
    End Sub

    Private Sub timer1_Tick(sender As Object, e As EventArgs)
        Dim diff As New TimeSpan()
        diff = DateTime.Now.Subtract(startTime)
        lblTime.Text = diff.Hours & ":" & diff.Minutes.ToString("00") & ":" & diff.Seconds.ToString("00")
        lblTime.Invalidate()
    End Sub
End Class

将一个 BackgroundWorker 控件拖放到表单上,这里是后台工作器事件:

Drag/Drop a BackgroundWorker control onto the form, here are the background worker events:

Private Sub backgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    e.Result = e.Argument
    'DirectCast(e.Result, ThinkingProgressBar).SetThinkingBar(True) 
    'DO LONG OPERATION HERE
    
End Sub

Private Sub backgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    Dim dlg As ThinkingProgressBar = TryCast(e.Result, ThinkingProgressBar)
    If IsNothing(dlg) = False Then
        dlg.SetThinkingBar(False)
        dlg.Close()
    End If
End Sub

这是应用程序启动和升级时的调用代码:

And here is the calling code for when your application starts and does the upgrading:

Dim dlg As New ThinkingProgressBar()
dlg.SetThinkingBar(True)
BackgroundWorker1.RunWorkerAsync(dlg)
dlg.ShowDialog()
If IsNothing(dlg.Tag) = False AndAlso dlg.Tag.ToString() = "Cancelled" Then
    Return
End If

有几件事,您可以阻止用户取消(即 lblClose_LinkClicked)并放入保护性/防御性编程以处理用户在升级过程中终止进程或关闭其 PC 的情况.

A couple of things, you may prevent the user from Cancelling (ie lblClose_LinkClicked) and put in protective/defensive programming to handle cases where the user kills the process or turns off their PC during the upgrade.

ProgressBar 实际上是一个动画 gif - 这将适合您的使用,因为估计更新数据库所需的时间是非常难以预测的:

And the ProgressBar is actually an animated gif - and this will suit your usage because estimating the time it takes to update a database is very hard to predict:

这篇关于VB.NET 进度条后台工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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