解决winform在运行过程假死,提升用户体验(适用于查询或者处理时间过长的过程)等待过程图

解决winform在运行过程假死,提升用户体验(适用于查询或者处理时间过长的过程)等待过程图_第1张图片

解决winform在运行过程假死,提升用户体验(适用于查询或者处理时间过长的过程)等待过程图

调用方法:
在过程中定义类,并将Task传递给对象T

    Dim t As Task
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim clsIndicator As New ClsIndicator
        t = Task.Run(Sub() TestProcess())
        clsIndicator.T = t
    End Sub

    Sub TestProcess()
        Threading.Thread.Sleep(10000)
    End Sub
Public Class ClsIndicator
   Private form As New Form
   Private Indicator As CCWin.SkinControl.SkinProgressIndicator
   Private Thr As Task
   Property IsStop As Boolean
   Sub New()
       Dim size As New Size(140, 140)
       form.Size = New Size(140, 140)
       form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
       form.StartPosition = FormStartPosition.CenterScreen
       form.TopMost = True
       form.BackColor = Color.White
       form.Opacity = 0.5
       Indicator = New CCWin.SkinControl.SkinProgressIndicator With {
       .AutoStart = True,
       .Size = New Size(140, 140),
       .CircleColor = Color.DeepPink,
       .TextDisplay = 2,
       .NumberOfCircles = 12,
       .NumberOfVisibleCircles = 10,
       .AnimationSpeed = 85,
       .CircleSize = 0.5,
       .Font = New Font("宋体", 12)
       }
       form.Controls.Add(Indicator)
   End Sub
   Dim thread As Threading.Thread
   Public WriteOnly Property T() As Task
       Set(ByVal value As Task)
           Thr = value
           thread = New Threading.Thread(AddressOf WhileThrading)
           thread.Start()
       End Set
   End Property
   Sub WhileThrading()
       form.Show()
       Dim ints As Integer
       IsStop = False
       Do While True
           Indicator.Text = ints / 1000 & "S"
           If Thr.IsCompleted Then
               form.Close()
               IsStop = True
               Exit Do
           End If
           Application.DoEvents()
           ints += 1
           Threading.Thread.Sleep(100)
       Loop
       Threading.Thread.CurrentThread.Abort()
   End Sub

End Class

c#代码

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;


public class ClsIndicator
{
    private Form form = new Form();
    private CCWin.SkinControl.SkinProgressIndicator Indicator;
    private Task Thr;
    public bool IsStop { get; set; }
    public ClsIndicator()
    {
        Size size = new Size(140, 140);
        form.Size = new Size(140, 140);
        form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        form.StartPosition = FormStartPosition.CenterScreen;
        form.TopMost = true;
        form.BackColor = Color.White;
        form.Opacity = 0.5;
        Indicator = new CCWin.SkinControl.SkinProgressIndicator()
        {
            AutoStart = true,
            Size = new Size(140, 140),
            CircleColor = Color.DeepPink,
            TextDisplay = 2,
            NumberOfCircles = 12,
            NumberOfVisibleCircles = 10,
            AnimationSpeed = 85,
            CircleSize = 0.5,
            Font = new Font("宋体", 12)
        };
        form.Controls.Add(Indicator);
    }
    private System.Threading.Thread thread;
    public Task T
    {
        set
        {
            Thr = value;
            thread = new System.Threading.Thread(WhileThrading);
            thread.Start();
        }
    }
    public void WhileThrading()
    {
        form.Show();
        int ints;
        IsStop = false;
        while (true)
        {
            Indicator.Text = ints / (double)1000 + "S";
            if (Thr.IsCompleted)
            {
                form.Close();
                IsStop = true;
                break;
            }
            Application.DoEvents();
            ints += 1;
            System.Threading.Thread.Sleep(100);
        }
        System.Threading.Thread.CurrentThread.Abort();
    }
}



**注意:需要引用 CSkin.dll**
![在这里插入图片描述](https://img-blog.csdnimg.cn/3c54037ba4c24c60a1ba0033e40d4dd6.png

你可能感兴趣的:(ux,c#,ui)