当有大量数据需要计算、显示在界面或者调用sleep函数时,容易导致界面卡死,可以采用多线程加委托的方法解决
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
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 WindowsFormsApplication1 { public partial class FormMain : Form { DataTable table; int currentIndex = 0; int max = 10000; public FormMain() { InitializeComponent(); } private void button1_Click( object sender, EventArgs e) { button1.Enabled = false ; Thread thread = new Thread( new ThreadStart(LoadData)); thread.IsBackground = true ; thread.Start(); progressBar1.Minimum = 0; progressBar1.Maximum = max; } private void LoadData() { SetLableText( "数据加载中..." ); currentIndex = 0; table = new DataTable(); table.Columns.Add( "id" ); table.Columns.Add( "name" ); table.Columns.Add( "age" ); while (currentIndex < max) { SetLableText( string .Format( "当前行:{0},剩余量:{1},完成比例:{2}%" , currentIndex, max - currentIndex, (Convert.ToDecimal(currentIndex) / Convert.ToDecimal(max) * 100).ToString( "f0" ))); SetPbValue(currentIndex); DataRow dr = table.NewRow(); dr[ "id" ] = currentIndex; string name = "张三" ; dr[ "name" ] = name; dr[ "age" ] = currentIndex + 5; table.Rows.Add(dr); currentIndex++; } SetDgvDataSource(table); SetLableText( "数据加载完成!" ); this .BeginInvoke( new MethodInvoker( delegate () { button1.Enabled = true ; })); } delegate void labDelegate( string str); private void SetLableText( string str) { if (label1.InvokeRequired) { Invoke( new labDelegate(SetLableText), new string [] { str }); } else { label1.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; } } delegate void pbDelegate( int value); private void SetPbValue( int value) { if (progressBar1.InvokeRequired) { Invoke( new pbDelegate(SetPbValue), new object [] { value }); } else { progressBar1.Value = value; } } } }<br>运行效果图: |

1
|
|
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2012-01-18 MongoDB学习笔记(一) MongoDB介绍及安装
2012-01-18 Entity Framework 4.1 : 基础
2012-01-18 批量清理指定数据库中所有数据--SqlServer
2012-01-18 Asp.net MVC 基于规则的权限设计
2011-01-18 微软Windows Azure Platform技术解析
2011-01-18 Web打印的在线设计
2011-01-18 Jla框架