VB.NET progressbar后台工作人员 [英] VB.NET progressbar backgroundworker

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

问题描述

当我的应用程序启动且刚刚升级时,我正在进行本地数据库更新(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.

这是ProgressBar表单:

This is the ProgressBar Form:

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 progressbar后台工作人员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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