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

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

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

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页