参考: http://www.cnblogs.com/xpvincent/archive/2013/08/19/3268001.html

当有大量数据需要计算、显示在界面或者调用sleep函数时,容易导致界面卡死,可以采用多线程加委托的方法解决;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading; namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
DataTable table;
int currentIndex = 0;
int max = 10000; public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
buttonOK.Enabled = false;
Thread thread = new Thread(new ThreadStart(LoadData));
thread.IsBackground = true;
thread.Start(); progressBar1.Minimum = 0;
progressBar1.Maximum = max;
} private void LoadData()
{
SetLabelText("数据加载中...");
currentIndex = 0;
table = new DataTable();
table.Columns.Add("id");
table.Columns.Add("name");
table.Columns.Add("age");
while (currentIndex < max)
{
SetLabelText(string.Format("当前行:{0},剩余量:{1},完成比例:{2}%",
currentIndex, max - currentIndex, (Convert.ToDecimal(currentIndex) / Convert.ToDecimal(max) * 100).ToString()));
SetPbValue(currentIndex);
DataRow dr = table.NewRow();
dr["id"] = currentIndex;
dr["name"] = "张三";
dr["age"] = currentIndex + 5;
table.Rows.Add(dr);
currentIndex++;
} SetDgvDataSource(table);
SetLabelText("数据加载完成"); this.BeginInvoke(new MethodInvoker(delegate()
{
buttonOK.Enabled = true;
}));
} delegate void labDelegate(string str);
private void SetLabelText(string str)
{
if (textBox1.InvokeRequired)
{
Invoke(new labDelegate(SetLabelText), new string[] { str });
}
else
{
textBox1.Text = str;
}
} delegate void dgvDelegate(DataTable table);
private void SetDgvDataSource(DataTable table)
{
if (dataGridView1.InvokeRequired)
{
Invoke(new dgvDelegate(SetDgvDataSource), new object[] { table });
}
else
{
dataGridView1.DataSource = table;
}
} private delegate void pbDelegate(int value);
private void SetPbValue(int value)
{
if (progressBar1.InvokeRequired)
{
Invoke(new pbDelegate(SetPbValue), new object[] { value }); }
else
{
progressBar1.Value = value;
}
} }
}

完整Demo下载

WinForm 多线程+委托来防止界面假死的更多相关文章

  1. WinForm查询大数据界面假死,使用异步调用解决

    用DataGridView无分页绑定一个几千条数据的查询,查询的时候界面直接卡死十几秒,用户体验非常不好,因此用异步操作解决界面卡死的问题原本场景:点击[查询]后,界面直接卡死优化场景:点击[查询]后 ...

  2. C#中异步及winform中界面假死

    c#中可以用BeginInvoke去启动异步调用,但是有两个BeginInvoke一个是controller的BeginInvoke还有一个是委托的BeginInvoke. 主要区别是controll ...

  3. WinForm程序界面假死,寻求完美解决方案

    故事的开端是这样的,小白是一个程序员,他确实也是一个小白,目前还在程序员发展的道路上,兢兢业业的小心求学. 有一天,小白接到一个任务,完成一个Winform程序,附加一个功能就是可以读IC卡. 小白终 ...

  4. C# Winform 窗体界面”假死”后台线程阻塞 解决办法–BeginInvoke

    原文:C# Winform 窗体界面"假死"后台线程阻塞 解决办法–BeginInvoke 这个方法可以用在任何后台任务耗时较长,造成界面“假死”界面控件不更新的情况. 比如要要执 ...

  5. Qt多线程和GUI界面假死(run()是线程的入口,就像main()对于应用程序的作用。分析QThread::exec函数的源码,旧的QMutexLocker模式其实很好用,挡住别人进入抢占资源,可照抄)good

    QThread的常见特性: run()是线程的入口,就像main()对于应用程序的作用.QThread中对run()的默认实现调用了exec(),从而创建一个QEventLoop对象,由其处理该线程事 ...

  6. 使用publisher模式控制频繁的UI输出,避免Winform界面假死

    http://www.cnblogs.com/Charltsing/p/publisher.html 最近测试task并发任务的效率与线程池的区别,发现了另外一个问题.task建立任务的速度很快,输出 ...

  7. 解决c# progressBar更新出现界面假死

    最近一个项目需求中的一个功能是需要用progressBar反映处理文件的进度. 研究了Invoke和BeginInvoke方法. Control.Invoke 方法 (Delegate) :在拥有此控 ...

  8. WPF界面假死

    首先要检查那些滥用 Timer.Dispacher Timer 或者滥用什么“线程+死循环+阻塞”轮询的代码. 这种是编程大忌,有些人不会设计事件驱动程序,而是滥用轮询. 若是:触发事件后的假死,搜W ...

  9. WinForm多线程及委托防止界面假死

    当有大量数据需要计算.显示在界面或者调用sleep函数时,容易导致界面卡死,可以采用多线程加委托的方法解决. using System; using System.Collections.Generi ...

  10. WinForm多线程+委托防止界面假死

    当有大量数据需要计算.显示在界面或者调用sleep函数时,容易导致界面卡死,可以采用多线程加委托的方法解决 using System; using System.Collections.Generic ...

随机推荐

  1. windows电脑在线生成ios p12证书工具和生成教程

    使用hbuilderx开发ios APP的时候,打包APP提示需要IOS的打包证书 而hbuilderx本身是不能生成证书的,因为生成证书需要在苹果开发者中心生成.而在苹果开发者中心生成证书的时候,提 ...

  2. 重磅推出 Sdcb Chats:一个全新的开源大语言模型前端

    重磅推出 Sdcb Chats:一个全新的开源大语言模型前端 在当前大语言模型(LLM)蓬勃发展的时代,各类 LLM 前端层出不穷.那么,为什么我们还需要另一个 LLM 前端呢? 最初的原因在于质感的 ...

  3. 从 CephFS 到 JuiceFS:同程旅游亿级文件存储平台构建之路

    随着公司业务的快速发展,同程旅行的非结构化的数据突破 10 亿,在 2022 年,同程首先完成了对象存储服务的建设.当时,分布式文件系统方面,同程使用的是 CephFS,随着数据量的持续增长,Ceph ...

  4. CVE-2023-31436 数组越界漏洞

    CVE-2023-31436 数组越界漏洞 drawio: CVE-2023-31436.drawio ‍ 漏洞分析 在 qfq_change_class 里面如果用户态没有提供 TCA_QFQ_LM ...

  5. android emulator 设置代理

    android emulator 设置代理 由于开发的 app 需要访问 google 服务,那么跑虚拟机的时候就需要设置网络代理,试了几种方法都没成功,记录一下 因为已知我开发电脑的代理地址和端口, ...

  6. Fake JSON Server

    Fake JSON Server https://github.com/ttu/dotnet-fake-json-server Fake JSON Server 是 Fake REST API,可以作 ...

  7. 04. PART 2 IdentityServer4 ASP.NET Core Identity .NET Core 3.1

    04. PART 2 IdentityServer4 ASP.NET Core Identity .NET Core 3.1 如果您已经来到这里,那么祝贺你的坚持,最难的部分已经完成了.我们仅仅需要的 ...

  8. d2js 中实现 memcached 共享 session 的过程

    https://github.com/inshua/d2js/blob/master/WebContent/guide/memcached-session.md 基于 https://github.c ...

  9. TypeNameExtractor could not be found

    TypeNameExtractor could not be foundException 一.注意: 如果项目中使用了 knif4j或swagger (knif4j内核中就使用了swagger) 项 ...

  10. Type of the default value for 'data' prop must be a function的解决方法

    Type of the default value for 'data' prop must be a function的解决方法 问题现象 在写形如prop: {type: Array; defau ...