How to: Make Thread-Safe Calls to Windows Forms Controls
2007-10-06 00:21
489 查看
How to: Make Thread-Safe Calls to Windows Forms Controls
URL: ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxmclictl/html/138f38b6-1099-4fd5-910c-390b41cbad35.htm
Access to Windows Forms controls is not inherently thread safe. If you have two or more threads manipulating the state of a control, it is possible to force the control into an inconsistent state. Other thread-related bugs are possible as well, including race conditions and deadlocks. It is important to ensure that access to your controls is done in a thread-safe way.
The .NET Framework helps you detect when you are accessing your controls in a manner that is not thread safe. When you are running your application in the debugger, and a thread other than the one which created a control attempts to call that control, the debugger raises an InvalidOperationException with the message, "Control control name accessed from a thread other than the thread it was created on."
This exception occurs reliably during debugging and, under some circumstances, at run time. You are strongly advised to fix this problem when you see it. You might see this exception when you debug applications that you wrote with the .NET Framework prior to .NET Framework version 2.0.
NOTE:
You can disable this exception by setting the value of the CheckForIllegalCrossThreadCalls property to false. This causes your control to run the same way as it would run under Visual Studio 2003.
For example,
public frmPromotionEmail()
{
InitializeComponent();
//Control.CheckForIllegalCrossThreadCalls = false;
}
If InvokeRequired returns true, call Invoke with a delegate that makes the actual call to the control.
If InvokeRequired returns false, call the control directly.
In the following code example, this logic is implemented in a utility method called
URL: ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxmclictl/html/138f38b6-1099-4fd5-910c-390b41cbad35.htm
Access to Windows Forms controls is not inherently thread safe. If you have two or more threads manipulating the state of a control, it is possible to force the control into an inconsistent state. Other thread-related bugs are possible as well, including race conditions and deadlocks. It is important to ensure that access to your controls is done in a thread-safe way.
The .NET Framework helps you detect when you are accessing your controls in a manner that is not thread safe. When you are running your application in the debugger, and a thread other than the one which created a control attempts to call that control, the debugger raises an InvalidOperationException with the message, "Control control name accessed from a thread other than the thread it was created on."
This exception occurs reliably during debugging and, under some circumstances, at run time. You are strongly advised to fix this problem when you see it. You might see this exception when you debug applications that you wrote with the .NET Framework prior to .NET Framework version 2.0.
NOTE:
You can disable this exception by setting the value of the CheckForIllegalCrossThreadCalls property to false. This causes your control to run the same way as it would run under Visual Studio 2003.
For example,
public frmPromotionEmail()
{
InitializeComponent();
//Control.CheckForIllegalCrossThreadCalls = false;
}
Thread-Safe Calls to a Windows Forms Control
To make a thread-safe call a Windows Forms control
Query the control's InvokeRequired property.If InvokeRequired returns true, call Invoke with a delegate that makes the actual call to the control.
If InvokeRequired returns false, call the control directly.
In the following code example, this logic is implemented in a utility method called
SetText. A delegate type named
SetTextDelegateencapsulates the
SetTextmethod. When the TextBox control's InvokeRequired returns true, the
SetTextmethod creates an instance of
SetTextDelegateand calls the form's Invoke method. This causes the
SetTextmethod to be called on the thread that created the TextBox control, and in this thread context the Text property is set directly.
// This event handler creates a thread that calls a // Windows Forms control in a thread-safe way. private void setTextSafeBtn_Click( object sender, EventArgs e) { this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe)); this.demoThread.Start(); } // This method is executed on the worker thread and makes // a thread-safe call on the TextBox control. private void ThreadProcSafe() { this.SetText("This text was set safely."); } [code]// This method demonstrates a pattern for making thread-safe // calls on a Windows Forms control. // // If the calling thread is different from the thread that // created the TextBox control, this method creates a // SetTextCallback and calls itself asynchronously using the // Invoke method. // // If the calling thread is the same as the thread that created // the TextBox control, the Text property is set directly. private void SetText(string text) { // InvokeRequired required compares the thread ID of the // calling thread to the thread ID of the creating thread. // If these threads are different, it returns true. if (this.textBox1.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); this.Invoke(d, new object[] { text }); } else { this.textBox1.Text = text; } } There is another way to make thread-safe calls to windows forms controls, Thread-safe calls with BackgroundWorker. Please go to the follows url to get more detail info. ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxmclictl/html/138f38b6-1099-4fd5-910c-390b41cbad35.htm
相关文章推荐
- How to: Make Thread-Safe Calls to Windows Forms Controls
- How to: Make Thread-Safe Calls to Windows Forms Controls
- How to: Make Thread-Safe Calls to Windows Forms Controls
- How to make gradient colored Controls in Windows Forms
- How to: Host Controls in Windows Forms DataGridView Cells(介绍如何在datagridview中自定义列)
- Make ObservableCollection to thread safe (Updated)
- 【摘自Devx】How to pass parameters to Thread in Windows Forms -- and get results back
- System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls = false;
- How to: Host Controls in Windows Forms DataGridView Cells (CalendarCell)
- Adding Controls to Windows Forms at Runtime
- 解决 vs2005编译错误提示 "how to make cross-thread calls"超简单实例
- Windows Forms Programming In C# 读书笔记 - 第八章 Controls
- Thread message loop for a thread with a hidden window? Make AllocateHwnd safe
- Node.js howto: make asynchronous callback from thread
- How to: Develop a Simple Windows Forms Control
- windows窗体程序中使用WPF控件 Host WPF Controls in Windows Forms Application
- Using Windows Forms Controls in Internet Explorer
- Applied Windows Live Custom Domains (3) - SOAP Calls to the Web Service
- How To Use the Windows Phone 8.1 GridView and ListView Controls
- Safe, Simple Multithreading in Windows Forms 3 Part