首页 > 其他分享 >WinForm中UI假死的解决方法

WinForm中UI假死的解决方法

时间:2023-09-27 12:45:55浏览次数:30  
标签:假死 System private Threading UI using WinForm

https://www.codenong.com/cs106719464/

 

 

WinForm中的UI假死其实是个老生常谈的问题了,但最近还是很多人问我该如何解决,所以今天就来说明一下如何解决UI假死的问题。实验程序界面如下图所示:
在这里插入图片描述

方法一:async + await + Task

首先看下面一段代码:

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // 开始
        private void btnStart_Click(object sender, EventArgs e)
        {
            string message = GetMessage();
            MessageBox.Show(message);
        }

        // 一个耗时任务
        private string GetMessage()
        {
            Thread.Sleep(10000);
            return "Hello World";
        }
    }
}

 

 

在上面的代码中,GetMessage()方法耗时10秒钟,如果你点击按钮,那么在10秒钟内窗体将处于假死状态。这种情况很常见,之所以会造成UI假死的原因也很简单:某个函数耗时太久。在遇见这种情况的时候,我们就可以考虑使用async + await + Task来解决,代码如下:

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // 开始
        private async void btnStart_Click(object sender, EventArgs e)
        {
            string message = await GetMessage();
            MessageBox.Show(message);
        }

        // 一个耗时任务
        private async Task<string> GetMessage()
        {
            return await Task<string>.Run(() =>
            {
                Thread.Sleep(10000);
                return "Hello World";
            });
        }
    }
}

运行之后点击按钮,你会发现UI没有假死,窗体可以随意拖动了。

方法二:使用BackgroundWorker组件

在很多时候,我们需要动态显示当前的程序执行进度,以便让用户了解程序已经执行到哪一步了。很多同志都会这么写:

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // 开始
        private void btnStart_Click(object sender, EventArgs e)
        {
            int max = pgbStatus.Maximum;
            for (int i = 1; i <= max; i++)
            {
                pgbStatus.Value++;
                Thread.Sleep(1000);
            }
        }
    }
}

 

 

功能确实是实现了,进度条能够显示当前执行的进度,可惜UI还是处于假死状态,所以用户体验还是不好。其实WinForm已经给我们提供了一个处理多线程任务的组件BackgroundWorker,使用它可以轻松让你的程序告别UI假死,如下图所示:
在这里插入图片描述
代码如下:

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.bgw.WorkerReportsProgress = true;
            this.bgw.WorkerSupportsCancellation = true;
            this.bgw.DoWork += DoWork;
            this.bgw.ProgressChanged += ProgressChanged;
            this.bgw.RunWorkerCompleted += RunWorkerCompleted;
        }

        // 开始
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (bgw.IsBusy)
            {
                return;
            }
            bgw.RunWorkerAsync();
        }

        // DoWork
        private void DoWork(object sender, DoWorkEventArgs e)
        {
            int max = pgbStatus.Maximum;
            for (int i = 1; i <= max; i++)
            {
                bgw.ReportProgress(i);
                Thread.Sleep(1000);
            }
        }

        // ProgressChanged
        private void ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            pgbStatus.Value = e.ProgressPercentage;
        }

        // RunWorkerCompleted
        private void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("完成");
        }
    }
}

运行程序点击按钮,你会发现UI没有处于假死状态,窗体可以随意拖动。

方法三:Task + 委托(回调函数)

首先需要明确一点:UI线程位于主线程,如果想要在子线程里更新UI状态,必须要将其切换到主线程,最后进行更新操作。UI控件一般会提供Invoke、InvokeRequired,其中InvokeRequired用于判断是否有子线程在更新UI控件,如果有则返回true,Invoke用于将控制权切换到UI线程,代码如下:

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // 开始
        private void btnStart_Click(object sender, EventArgs e)
        {
            Task task = Task.Run(() =>
            {
                int max = pgbStatus.Maximum;
                for (int i = 1; i <= max; i++)
                {
                    UpdateValue(i);
                    Thread.Sleep(1000);
                }
            });
        }

        // 处理线程
        private void UpdateValue(int num)
        {
            if (pgbStatus.InvokeRequired)
            {
                pgbStatus.Invoke(new Action<int>(UpdateValue), new object[] { num });
            }
            else
            {
                pgbStatus.Value = num;
            }
        }
    }
}

 

 

方法三也可以解决UI的假死问题,当然也不一定要用Task,利用Thread也可以实现一样的效果。

 

 

标签:假死,System,private,Threading,UI,using,WinForm
From: https://www.cnblogs.com/zkwarrior/p/17732427.html

相关文章

  • element ui踩坑
    项目适配需要rem转换,但UI组件内部未转换问题:由于组件内,某些组件宽度和高度是通过prop传参,然后对行内样式动态赋值,所以单位还是px网上找的方法:将elementui的github源码拉下来,然后修改组件源码,然后打包,然后打补丁替换lib文件夹。个人觉得太繁琐,问题在于,所有的组件都得适配,......
  • uni-ui组件使用
    由于uni-app独特的easycom技术,可以免引用、注册,直接使用各种符合规则的vue组件。如果你没有创建uni-ui项目模板,也可以在你的工程里,通过uni_modules单独安装需要的某个组件比如找到uni-icon的下载:https://ext.dcloud.net.cn/plugin?name=uni-icons右侧点使用HBuilderX导入......
  • OpenHarmony装饰指定自定义组件:@BuilderParam装饰器
     当开发者创建了自定义组件,并想对该组件添加特定功能时,例如在自定义组件中添加一个点击跳转操作。若直接在组件内嵌入事件方法,将会导致所有引入该自定义组件的地方均增加了该功能。为解决此问题,ArkUI引入了@BuilderParam装饰器,@BuilderParam用来装饰指向@Builder方法的变量,开......
  • element-ui plus 修改对话框的样式,无效
    <el-dialogv-model="dialogVisible"title="Tips"width="30%":before-close="handleClose"append-to-body><span>Thisisamessage</span><template#footer>......
  • lombok注解:@Builder
    带有注释的方法@Builder(从现在起称为target)会导致生成以下7个内容:名为的内部静态类FooBuilder,具有与静态方法相同的类型参数(称为builder)。在构建器中:目标的每个参数都有一个私有非静态非最终字段。在构建器中:包私有无参数空构造函数。在构建器中:目标的每个参数都有一个类......
  • 一些常见小程序的UI设计分享
    外卖优惠券小程序的UI设计电子商城系统UI分享A B  C ......
  • element ui 的picker-option 30天前限制和30天后限制
    pickerOptionsStart:{disabledDate:(time)=>{//获取当前日期并减少30天//console.log(time,'..........tiem');//console.log(time,'..........tiem');if(this.endDate!=''){//co......
  • ERROR: Could not find a version that satisfies the requirement selunium (from ve
    错误信息ERROR:Couldnotfindaversionthatsatisfiestherequirementselenium(fromversions:none)ERROR:Nomatchingdistributionfoundforselenium解决方案方法1:增大超时时间pip--default-timeout=100installselenium方法2:修改安装源为清华安装源pipi......
  • UGUI 优化
    UI更新Canvas.SendWillRenderCanvases--UI更新耗时color 颜色normal 法线position 顶点位置包括uisizeAnchorsPivot(缩放平移旋转不影响)tangent 切线uv0 网格第一个纹理坐标uv1.....替换图片文本优化减少图片切换减少颜色变化顶点位......
  • NanUI网络拦截
    publicclassMyResponseFilter:CefResponseFilter{privateMemoryStreamoutputStream=newMemoryStream();///<summary>//////</summary>///<paramname="dataIn">数据输入</param&g......