Manipulating controls internal SS threads

Source: Internet
Author: User
ArticleDirectory
    • Limit cross-thread CILS
    • Proper Method
    • Sample Code
    • Anonymous delegates

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 SS 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.

Limit cross-thread CILS

Using alling method callfrom one thread and sending them processing SS thread boundaries to another thread is expensive in terms of system resources. at best, the performance of your application may suffer. at worst, conditions cocould occur that cause a deadlock in your application and freeze execution. hence, you shoshould limit repeated callto controls on other threads.

However, there are times when it's necessary to manipulate controls wait SS 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 Processing SS threads. For this example, assume you have defined a form that includes des a label control:


Public Partial Class Myform:Form{Private Label Label_status;}

To set the label's text into SS 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 callto 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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.