运行WinForm程序时,如果后台执行比较费时的操作,前天UI就会假死卡住,很影响使用感受,这里我们简单的解决一下这个问题

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WinForm
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
} /// <summary>
/// 后台要执行的操作
/// </summary>
private void AAA()
{
for (int i = ; i < ; i++) //模拟耗时的操作
{
if (i % == ) //每隔9999999次循环ui更新下百分比
{
this.label1.Invoke((Action<int>)delegate(int a) //在控件对象所在的线程上执行委托
{
this.label1.Text = a.ToString() + "%";
}, i / );
}
}
} private void button1_Click(object sender, EventArgs e)
{
((Action)AAA).BeginInvoke(null, null); //调用委托的异步执行方法,回调函数为空
}
}
}

最新文章

  1. WinServer2003 文件夹共享 方法设置
  2. POJO, DTO, VO, JavaBean的区别
  3. JAVA的自动装箱拆箱
  4. jxl 导入excel以及日期格式处理
  5. WCF架构日记-1
  6. Swift语法基础入门三(函数, 闭包)
  7. python计算文件夹大小(linux du命令 简化版)
  8. MACD指标
  9. Can&#39;t update: no tracked branch No tracked branch configured for branch dev.
  10. [linux]CentOS 7 下安装 RabbitMQ
  11. python 排序之sort
  12. JArray数组转换为DataTable
  13. 3,fiddler手机端的设置
  14. Play XML Entities
  15. Git删除本地修改
  16. WebAPI实例--第一个API
  17. dp练习2
  18. web项目文件夹上传
  19. 了解java虚拟机—非堆相关参数设置(4)
  20. unity, 模拟退后台

热门文章

  1. jenkins持续集成(一): 在Linux下的安装与配置
  2. 【串线篇】Mybatis之缓存原理
  3. 什么是网站TDK?
  4. vuejs如何调试代码
  5. js 运动框架-轻量级
  6. struts2---访问WEB
  7. 四轴PID思路整理
  8. 【系统架构理论】一篇文章精通:Spring Cloud Netflix Eureka
  9. 项目搭建(一):windows UIAutomation API 框架
  10. laravel 简单应用 redis