Manipulating Controls Across Threads(转载)
2011-01-15 23:20
387 查看
Methods that affect a Windows Forms control can be executed only on the thread that created the control. .NET does not permit directly manipulating controls across threads. The Visual Studio compiler under .NET 2.0 will mark these attempts as errors. .NET 1.1 will allow them, but these will often result in unexpected behavior like incorrectly-painted controls.
However, there are times when it's necessary to manipulate controls across threads. For example, you might disable a button or update a form in response to action taken by a separate thread.
To access a .NET control from another thread, you must use the Invoke method as follows:
Before accessing a .NET control, check the control's InvokeRequired property.
If true, call the control's Invoke property with the proper arguments. This invokes your code in the same thread as the control.
If false, you can access the control's properties directly.
To set the label's text across threads, add the following method to the MyForm class:
http://www.csharp411.com/manipulating-controls-across-threads/
Limit Cross-Thread Calls
Marshalling method calls from one thread and sending them across thread boundaries to another thread is expensive in terms of system resources. At best, the performance of your application may suffer. At worst, conditions could occur that cause a deadlock in your application and freeze execution. Hence, you should limit repeated calls to controls on other threads.However, there are times when it's necessary to manipulate controls across threads. For example, you might disable a button or update a form in response to action taken by a separate thread.
Proper Method
The .NET Framework provides methods that are safe to call from any thread for invoking methods that manipulate controls created by other threads.To access a .NET control from another thread, you must use the Invoke method as follows:
Before accessing a .NET control, check the control's InvokeRequired property.
If true, call the control's Invoke property with the proper arguments. This invokes your code in the same thread as the control.
If false, you can access the control's properties directly.
Sample Code
The following code demonstrates how you can access a .NET control across threads. For this example, assume you have defined a Form that includes a Label control:To set the label's text across threads, add the following method to the MyForm class:
public void SetStatus( string text ) { if (this.Label_Status.InvokeRequired) { this.Label_Status.BeginInvoke( new MethodInvoker( delegate() { SetStatus( text ); } ) ); } else { this.Label_Status.Text = text; } }
Anonymous Delegates
The key to this example is the use of anonymous delegates and the MethodInvoker method. The anonymous delegate eliminates the need to formally define a separate delegate. MethodInvoker provides a simple delegate to invoke a method with a void parameter list. This delegate can be used when making calls to a control's Invoke method, or when you need a simple delegate but do not want to define one yourself.http://www.csharp411.com/manipulating-controls-across-threads/
相关文章推荐
- #501 – 在对个控件中共享事件处理程序(Sharing an Event Handler Across Multiple Controls)
- RadControls for Silverlight(学习1-GridView) (转载)
- Manipulating Pages and Server Controls with JavaScript(使用JS处理页面和服务器控件)
- 【转载】Contiki 的protothreads
- Programming With POSIX Threads 读书笔记(三)——流水线技术[转载]
- Delphi 6, 7 threads synchronization in ActiveX controls
- Hiding/Manipulating Databound Items(转载www.aspallia
- Silverlight:telerik RadControls中RadGridView的一个Bug及解决办法(转载)
- ASP.Net Controls 用法大全 【转载】
- ASP.Net Controls 用法大全 【转载】
- 转载 在WPF中使用Microsoft Chart Controls (MSchart)
- Silverlight Toolkit Overview, Part 1 - Controls (转载)
- 利用 ProtoThreads实现Arduino多线程处理1(转载)
- [Android] DDMS查看Threads情况--转载
- Community Server专题附录一: 什么是Threads & Processes(转载)
- 转载:How can I drag and drop items between data bound ItemsControls?
- Exception doesn't travel across threads...
- Notifications Across Threads (跨线程的通知)
- Inside ASP.NET 2.0 – Controls Model(转载)
- 【转载】JSP上传图片并生成缩略图