您的位置:首页 > Web前端

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;
}

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
SetTextDelegate
encapsulates the
SetText
method. When the TextBox control's InvokeRequired returns true, the
SetText
method creates an instance of
SetTextDelegate
and calls the form's Invoke method. This causes the
SetText
method 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: