BeginInvoke 提高UI的响应速度
2007-05-08 10:17
363 查看
using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8
9using System.Threading;
10
11namespace CSharpGeriac
12using System;
2using System.Drawing;
3using System.Threading;
4using System.Windows.Forms;
5
6class Program : Form
7{
8 private System.Windows.Forms.ProgressBar _ProgressBar;
9
10 [STAThread]
11 static void Main()
12 {
13 Application.Run(new Program());
14 }
15
16 public Program()
17 {
18 InitializeComponent();
19 ThreadStart threadStart = Increment;
20 threadStart.BeginInvoke(null, null);
21 }
22
23 void UpdateProgressBar()
24 {
25 if (_ProgressBar.InvokeRequired)
26 {
27 MethodInvoker updateProgressBar = UpdateProgressBar;
28 _ProgressBar.Invoke(updateProgressBar);
29 }
30 else
31 {
32 _ProgressBar.Increment(1);
33 }
34 }
35
36 private void Increment()
37 {
38 for (int i = 0; i < 100; i++)
39 {
40 UpdateProgressBar();
41 Thread.Sleep(100);
42 }
43 if (InvokeRequired)
44 {
45 // Close cannot be called directly from
46 // a non-UI thread.
47 Invoke(new MethodInvoker(Close));
48 }
49 else
50 {
51 Close();
52 }
53 }
54
55 private void InitializeComponent()
56 {
57 _ProgressBar = new ProgressBar();
58 SuspendLayout();
59
60 _ProgressBar.Location = new Point(13, 17);
61 _ProgressBar.Size = new Size(267, 19);
62
63 ClientSize = new Size(292, 53);
64 Controls.Add(this._ProgressBar);
65 Text = "Multithreading in Windows Forms";
66 ResumeLayout(false);
67 }
68}
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8
9using System.Threading;
10
11namespace CSharpGeriac
12using System;
2using System.Drawing;
3using System.Threading;
4using System.Windows.Forms;
5
6class Program : Form
7{
8 private System.Windows.Forms.ProgressBar _ProgressBar;
9
10 [STAThread]
11 static void Main()
12 {
13 Application.Run(new Program());
14 }
15
16 public Program()
17 {
18 InitializeComponent();
19 ThreadStart threadStart = Increment;
20 threadStart.BeginInvoke(null, null);
21 }
22
23 void UpdateProgressBar()
24 {
25 if (_ProgressBar.InvokeRequired)
26 {
27 MethodInvoker updateProgressBar = UpdateProgressBar;
28 _ProgressBar.Invoke(updateProgressBar);
29 }
30 else
31 {
32 _ProgressBar.Increment(1);
33 }
34 }
35
36 private void Increment()
37 {
38 for (int i = 0; i < 100; i++)
39 {
40 UpdateProgressBar();
41 Thread.Sleep(100);
42 }
43 if (InvokeRequired)
44 {
45 // Close cannot be called directly from
46 // a non-UI thread.
47 Invoke(new MethodInvoker(Close));
48 }
49 else
50 {
51 Close();
52 }
53 }
54
55 private void InitializeComponent()
56 {
57 _ProgressBar = new ProgressBar();
58 SuspendLayout();
59
60 _ProgressBar.Location = new Point(13, 17);
61 _ProgressBar.Size = new Size(267, 19);
62
63 ClientSize = new Size(292, 53);
64 Controls.Add(this._ProgressBar);
65 Text = "Multithreading in Windows Forms";
66 ResumeLayout(false);
67 }
68}
相关文章推荐
- 暂时提高UI响应速度的几行代码
- winform下提高control在UI中的响应速度
- Android性能优化之提高应用响应速度(了解ViewStub和推迟视图的初始化)
- 提高AJAX客户端响应速度(转转)
- PHP使用memcache缓存技术提高响应速度的方法
- BeginInvoke更新界面 UI死掉 WPF
- PHP利用fastcgi_finish_request()函数实现异步操作,提高响应速度
- 使用fastcgi_finish_request提高页面响应速度
- 如何对SharePoint网站进行预热(warmup)以提高响应速度
- 在jsp页面中响应速度提高的7种方法分享
- 提高tomcat的响应速度
- android提高应用启动速度_splash页面瞬间响应_避免APP启动闪白屏
- 实时显示iOS编写UI代码效果:有效提高UI编写速度
- 深入浅出Symfony2 - 如何提高网站响应速度
- AJAX 客户端响应速度提高分析
- 提高网站响应速度的前端技术
- 使用ajaxcontroltoolkit时如何提高响应速度和阻止timeout
- [转]WinForms UI Thread Invokes: An In-Depth Review of Invoke/BeginInvoke/InvokeRequred
- c#多线程(UI线程,控件显示更新) Invoke和BeginInvoke 区别
- 将css和js缓存到localStorage缓存,提高网页响应速度