在VB.net中WinForm+Backgroundworker+StatusStrip范例

VSTO中应用的例子,希望给大家有点启发

在VB.net中WinForm+Backgroundworker+StatusStrip范例

6179acedb36e677cae2fc1ea3d2d5d36.png

以上是在VSTO的窗体

下面是代码部分

Imports System.ComponentModel


Public Class Form_backgroundworker
    Dim ShowFlag As Boolean = True
    Dim actCell As Excel.Range
    Private Sub Form_backgroundworker_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CenterToScreen()
        BackgroundWorker1.WorkerReportsProgress = True
        BackgroundWorker1.WorkerSupportsCancellation = True
    End Sub
    Private Sub Form_backgroundworker_Closed(sender As Object, e As EventArgs) Handles Me.Closed
        If BackgroundWorker1.IsBusy Then
            ShowFlag = False
            BackgroundWorker1.CancelAsync()
        End If
    End Sub
    Private Sub BackgroundWorker1_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        For j As Integer = 1 To 100
            If BackgroundWorker1.CancellationPending Then
                e.Cancel = True
                Return
            End If
            Try
                actCell.Offset(j - 1).Value = j
            Catch ex As Exception


            End Try
            worker.ReportProgress(j)
            System.Threading.Thread.Sleep(200)
        Next j
    End Sub


    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Dim changeNum As Integer = e.ProgressPercentage
        Dim ss As String = "发送中..." + changeNum.ToString() + "/100"
        If ShowFlag Then
            Me.Invoke(Sub()
                          ToolStripStatusLabel1.Text = ss
                          ToolStripProgressBar1.Value = changeNum
                          Label2.Text = ss
                      End Sub
                      )


        End If
    End Sub


    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Dim outStr As String = String.Empty
        If e.Error IsNot Nothing Then
            outStr = e.Error.Message
        ElseIf e.Cancelled Then
            outStr = "任务已取消"
        Else
            outStr = "完成"
        End If
        If ShowFlag Then
            Me.Invoke(Sub()
                          ToolStripStatusLabel1.Text = outStr
                          Label2.Text = outStr
                          Button1.Enabled = True
                      End Sub
                      )


        End If


    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Button1.Enabled = False
        ToolStripProgressBar1.Maximum = 100
        ToolStripProgressBar1.Minimum = 0
        ToolStripProgressBar1.Value = 0
        ShowFlag = True
        If Not BackgroundWorker1.IsBusy Then
            actCell = xlapp.ActiveCell
            BackgroundWorker1.RunWorkerAsync()
        End If
    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If BackgroundWorker1.IsBusy Then
            ShowFlag = False
            BackgroundWorker1.CancelAsync()
        End If
    End Sub
End Class

657b242a5b99997d2ee10cf43813621c.png

4ab983c1d98c800f072e64d457b55eeb.png

610da54367120f6a91868d57e2ed893b.png

========================

关注订阅号(公众号),有更多的文章

========================

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值