WinForm中多线程操作控件
2008-08-16 11:58
302 查看
在WinForm中的多线程中,操作控件时,会报无效操作异常,解决方法是调用System.Windows.Form.Control中BeginInvoke。
代码如下:
Code
1 /// <summary>
2 /// Operate WinForm Control In Theard.
3 /// <remarks>author PetterLiu http://wintersun.cnblogs.com</remarks>
4 /// </summary>
5 public partial class ThreadFormTest : Form
6 {
7 private Thread thread;
8 private delegate void SetButtonText();
9 private delegate void SetButtonWithNewText(string txt);
10
11 public ThreadFormTest()
12 {
13 InitializeComponent();
14 }
15
16 public void ChangeButtonText()
17 {
18 MessageBox.Show(Thread.CurrentThread.ManagedThreadId.ToString());
19 this.button1.Text = DateTime.Now.ToLongTimeString();
20 }
21
22 public void ChangeButtonText2()
23 {
24 MessageBox.Show(Thread.CurrentThread.ManagedThreadId.ToString());
25 SetButtonText setbuttontext = new SetButtonText(ChangeButtonText);
26 this.button1.BeginInvoke(setbuttontext);
27 }
28
29 public void ChangeButtonWithNText()
30 {
31 MessageBox.Show(Thread.CurrentThread.ManagedThreadId.ToString());
32 SetButtonWithNewText setbuttontext = new SetButtonWithNewText(CHKNASD);
33 this.button1.BeginInvoke(setbuttontext, new object[] { "It's deliver value." });
34 }
35
36 private void CHKNASD(string txt)
37 {
38 this.button1.Text = txt;
39 }
40
41 private void button1_Click(object sender, EventArgs e)
42 {
43 thread = new Thread(new ThreadStart(ChangeButtonText));
44 thread.Start();
45 //thow InvaildOperationException
46 }
47
48 private void button2_Click(object sender, EventArgs e)
49 {
50 thread = new Thread(new ThreadStart(ChangeButtonText2));
51 thread.Start();
52 }
53
54 private void button3_Click(object sender, EventArgs e)
55 {
56 //implement deliver value.
57 thread = new Thread(new ThreadStart(ChangeButtonWithNText));
58 thread.Start();
59 }
60 }
再看《CLR.via.C#.Second.Edition.Feb.2006》中是这么说,这下清楚了。
In Windows, a window is always created by a thread, and this thread must be used to process
all actions for the window. One reason for this is because 16-bit Windows versions were sin-
gle-threaded operating systems, and in order to maintain backward compatibility, 32-bit and 64-
bit versions of Windows kept the single-threaded architecture for handling window opera-
tions such as WM_MOVE, WM_SIZE, WM_PAINT, WM_CLOSE, etc. It's common for a Windows Forms
application to use the asynchronous techniques explained in this chapter. However, since Win-
dows Forms is built on top of Windows, a thread pool thread is not allowed to directly manipu-
late a window; or more specifically, a class derived from System.Windows.Forms.Control.
Fortunately, the System.Windows.Forms.Control class offers three methods—Invoke, Begin-
Invoke, and EndInvoke-that you can call from any thread (including a thread pool thread) to
marshal an operation from the calling thread to the thread that created the window. Inter-
nally, Control's Invoke method calls the Win32 SendMessage method to have the window's
thread execute a task synchronously. Control's BeginInvoke method internally calls the
Win32 PostMessage method to have the window's thread execute a task asynchronously. If
the calling thread wants to know when the window's thread has completed executing the
task, it can call Control's EndInvoke method. If you don't care, this is one of the rare cases
when you do not have to call an EndInvoke method.
代码如下:
Code
1 /// <summary>
2 /// Operate WinForm Control In Theard.
3 /// <remarks>author PetterLiu http://wintersun.cnblogs.com</remarks>
4 /// </summary>
5 public partial class ThreadFormTest : Form
6 {
7 private Thread thread;
8 private delegate void SetButtonText();
9 private delegate void SetButtonWithNewText(string txt);
10
11 public ThreadFormTest()
12 {
13 InitializeComponent();
14 }
15
16 public void ChangeButtonText()
17 {
18 MessageBox.Show(Thread.CurrentThread.ManagedThreadId.ToString());
19 this.button1.Text = DateTime.Now.ToLongTimeString();
20 }
21
22 public void ChangeButtonText2()
23 {
24 MessageBox.Show(Thread.CurrentThread.ManagedThreadId.ToString());
25 SetButtonText setbuttontext = new SetButtonText(ChangeButtonText);
26 this.button1.BeginInvoke(setbuttontext);
27 }
28
29 public void ChangeButtonWithNText()
30 {
31 MessageBox.Show(Thread.CurrentThread.ManagedThreadId.ToString());
32 SetButtonWithNewText setbuttontext = new SetButtonWithNewText(CHKNASD);
33 this.button1.BeginInvoke(setbuttontext, new object[] { "It's deliver value." });
34 }
35
36 private void CHKNASD(string txt)
37 {
38 this.button1.Text = txt;
39 }
40
41 private void button1_Click(object sender, EventArgs e)
42 {
43 thread = new Thread(new ThreadStart(ChangeButtonText));
44 thread.Start();
45 //thow InvaildOperationException
46 }
47
48 private void button2_Click(object sender, EventArgs e)
49 {
50 thread = new Thread(new ThreadStart(ChangeButtonText2));
51 thread.Start();
52 }
53
54 private void button3_Click(object sender, EventArgs e)
55 {
56 //implement deliver value.
57 thread = new Thread(new ThreadStart(ChangeButtonWithNText));
58 thread.Start();
59 }
60 }
再看《CLR.via.C#.Second.Edition.Feb.2006》中是这么说,这下清楚了。
In Windows, a window is always created by a thread, and this thread must be used to process
all actions for the window. One reason for this is because 16-bit Windows versions were sin-
gle-threaded operating systems, and in order to maintain backward compatibility, 32-bit and 64-
bit versions of Windows kept the single-threaded architecture for handling window opera-
tions such as WM_MOVE, WM_SIZE, WM_PAINT, WM_CLOSE, etc. It's common for a Windows Forms
application to use the asynchronous techniques explained in this chapter. However, since Win-
dows Forms is built on top of Windows, a thread pool thread is not allowed to directly manipu-
late a window; or more specifically, a class derived from System.Windows.Forms.Control.
Fortunately, the System.Windows.Forms.Control class offers three methods—Invoke, Begin-
Invoke, and EndInvoke-that you can call from any thread (including a thread pool thread) to
marshal an operation from the calling thread to the thread that created the window. Inter-
nally, Control's Invoke method calls the Win32 SendMessage method to have the window's
thread execute a task synchronously. Control's BeginInvoke method internally calls the
Win32 PostMessage method to have the window's thread execute a task asynchronously. If
the calling thread wants to know when the window's thread has completed executing the
task, it can call Control's EndInvoke method. If you don't care, this is one of the rare cases
when you do not have to call an EndInvoke method.
相关文章推荐
- C# winform编程中多线程操作控件方法
- WinForm解决多线程操作控件问题 线程间操作无效: 从不是创建控件的线程访问它"
- Winform中多线程的控件访问引发的“线程间操作无效,从不是创建控件的线程访问它”
- Winform中进行线程安全的多线程操作(主线程控件,非backgroundWorker)
- c#中使用多线程访问winform中控件的若干问题 解决线程间操作无效: 从不是创建控件的线程访问它
- winform 多线程执行操作,并跨线程操作控件
- c#有关winform的两个代码片段(多线程操作窗体控件与窗体淡入淡出效果)
- [转]c#有关winform的两个代码片段(多线程操作窗体控件与窗体淡入淡出效果)
- [转] c#有关winform的两个代码片段(多线程操作窗体控件与窗体淡入淡出效果)
- 一个WinForm记事本程序(包含主/下拉/弹出菜单/打开文件/保存文件/打印/页面设置/字体/颜色对话框/剪切版操作等等控件用法以及记事本菜单事件/按键事件的具体代码)
- 解决多线程操作控件时可能出现的异常:“在某个线程上创建的控件不能成为在另一个线程上创建的控件的父级”
- 多线程操作控件属性
- c# winform 多线程操作统一对象(lock)
- 关于多线程委托的控件操作
- (委托事件处理)关于多线程执行显示进度条的实例(转)&&线程间操作无效: 从不是创建控件“rtxtEntryNO”的线程访问它。
- 基于C#的WinForm中DataGridView控件操作汇总
- 一个WinForm记事本程序(包含主/下拉/弹出菜单/打开文件/保存文件/打印/页面设置/字体/颜色对话框/剪切版操作等等控件用法以及记事本菜单事件/按键事件的具体代码)
- 多线程操作控件C#
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- Winform 的dadagridview控件的修改操作