VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • VB.NET学习笔记:多线程加委托实现等待窗体(loading正在加载界面),运行超时可

在《异步委托实现等待窗体(loading界面),执行任务超时可以取消操作》一文,通过异步委托貌视能很好的实现了所要的效果,但因异步委托无法控制线程进度,也就是说不能人工干预线程,所以取消操作就是一摆设,存在BUG,本文通过多线程修改了该BUG。
正在加载界面
取消操作
等待窗体WaitForm代码如下:

Imports System.Threading

Public Class WaitForm
    Private _MaxWaitTime As Integer
    Private _WaitTime As Integer
    Private _CancelEnable As Boolean
    Private _AsyncResult As IAsyncResult
    Private _method As MethodInvoker
    Dim th As Thread
    Public Property Message As String
    Public Property TimeSpan As Integer


    Public Sub New(ByVal method As MethodInvoker, ByVal maxWaitTime As Integer, ByVal waitMessage As String, ByVal cancelEnable As Boolean, ByVal timerVisable As Boolean)
        maxWaitTime *= 1000
        Initialize(method, maxWaitTime, waitMessage, cancelEnable, timerVisable)
    End Sub

    Private Sub Initialize(ByVal method As MethodInvoker, ByVal maxWaitTime As Integer, ByVal waitMessage As String, ByVal cancelEnable As Boolean, ByVal timerVisable As Boolean)
        InitializeComponent()
        '无边框
        Me.FormBorderStyle = FormBorderStyle.None
        '起始位于父窗体中间
        Me.StartPosition = FormStartPosition.CenterParent
        '不在任务栏显示
        Me.ShowInTaskbar = False
        '窗体透明区域颜色为窗体背景色
        Me.TransparencyKey = Me.BackColor
        Me.labMessage.Text = waitMessage
        TimeSpan = 500
        Message = String.Empty
        _CancelEnable = cancelEnable
        _MaxWaitTime = maxWaitTime
        _WaitTime = 0
        _method = method
        Me.LikCancel.Visible = _CancelEnable
        Me.labTimer.Visible = timerVisable
        Me.Timer1.Interval = TimeSpan
        Me.Timer1.Start()
    End Sub

    Private Sub LikCancel_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LikCancel.LinkClicked
        Me.Message = "您结束了当前操作!"
        Me.Close()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        _WaitTime += TimeSpan
        Me.labTimer.Text = String.Format("{0}秒", _WaitTime / 1000)

        If _WaitTime > _MaxWaitTime Then
            Message &= String.Format("处理数据超时{0}秒,结束当前操作!", _MaxWaitTime / 1000)

            Me.Close()
        End If

        If th.ThreadState = ThreadState.Stopped Then
            Message &= "操作完成!"
            Me.Close()
        End If
    End Sub

    Private Sub LoadForm_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        th = New Thread(AddressOf ab)
        th.IsBackground = True
        th.SetApartmentState(ApartmentState.STA)
        th.Start()
    End Sub

    Private Sub ab()
        _method.Invoke()
    End Sub


    Private Sub WaitForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If th.ThreadState <> ThreadState.Stopped Then
            th.Abort()
        End If
    End Sub
End Class
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80

这里使用Thread的ThreadState属性判断线程是否运行结束,如果运行结束则关闭等待窗体,如果用户提前取消操作则通过Thread的Abort方法结束进程运行。 更多阅读: 1、为何要制作等待窗体:程序长时间执行任务时窗体会失去响应造成假死避免重复点击按钮的解决思路 2、等待窗体的具体制作步骤:异步委托实现等待窗体(loading界面),执行任务超时可以取消操作

相关资源可以到下载频道下载:https://download.csdn.net/user/zyjq52uys/uploads


相关教程