In the winfrom application, if the background thread is used to operate the interface, the control. Invoke () method should be called.

Source: Internet
Author: User
In the winfrom application, if you need to execute a long call or use web serives to retrieve some data from the server, we usually use multiple threads to perform this operation, after the operation is complete, modify the status of some controls. for example Code When you click the button, the button and text box will be grayed out. After the operation is complete, set the button and text box to available in the callback function. Using System;
Using System. drawing;
Using System. Threading;
Using System. Windows. forms;

Class App
{< br> Public static void main ()
{< br> application. run ( New multithreadedform ());
}
}

Class Multithreadedform: Form
{

Public Multithreadedform ()
{

// Create a textbox
Text. Location =   New Point ( 10 , 10 );
Text. Size =   New Size ( 50 , 20 );
Controls. Add (text );

// Create a button
Button. Text =   " BEEP " ;
Button. Size =   New Size ( 50 , 20 );
Button. Location =   New Point ( 80 , 10 );

// Register Click Event Handler
Button. Click + =   New Eventhandler (onclick );

Controls. Add (button );
}

Void Onclick (Object sender, eventargs ARGs)
{
Enablecontrols (False);
Thread mythreading= NewThread (NewSystem. Threading. threadstart (callback ));
Mythreading. Start ();
}


Void Callback ()
{
//Dosomething ();
//System. Threading. thread. Sleep (5000 );
//MessageBox. Show ("OK ");
Enablecontrols (True);
}

Void Enablecontrols (Boolean enable)
{
Button. Enabled=Enable;
Text. Enabled=Enable;
}

Button button =   New Button ();
Textbox text =   New Textbox ();
}

The above code can usually run normally, but there is a potential problem. The callback method does not run on the same thread as the calling code, which means it may be executing the application. Program Execute the other part simultaneously. to avoid this problem, you need to use synchronization (such as lock) when accessing objects that may be used by some programs. However, this locking control is not suitable for exclusive access, instead, it will cause more problems. In this case, we need to use control. invoke () method.
The invoke () method is defined in system. windows. forms. control, all from ssytem. windows. forms. the class inherited by control includes the from method. as we all know, the winfrom class library is implemented on the basis of Win32 API, while the invoke () method is implemented by the postmessage function at the underlying layer, that is, the invoke () the method is equivalent to sending a message to the target object. The control operation is still completed by the user interface thread. Therefore, this method is thread-safe. for the above Code, you only need to make some modifications
// Declare a delegate
Delegate booleancallback (bool enable );
Void booleancallback enablecontrols;
// Initialize In the constructor
Enablecontrols = new booleancallback (enablecontrols );
// Change enablecontrols (true)
Invoke (enablecontrols, new object [] {true });
Or
Create a help class Public   Class Updater
{
Private Textbox;
Private Button button;
Private   Bool Enabled;

Public Updater (textbox Textbox, button, Bool Enabled)
{
This. Textbox=Textbox;
This. Button=Button;
This. Enabled=Enabled;
}

Public   Void Update ()
{
This. Textbox. Enabled=Enabled;
This. Button. Enabled=Enabled;
}
}

Then change enablecontrls (true)
// Create an Updater object
Updater = new Updater (this. text, this. Button, true );
// Call the invoke () method
Invoke (New methodinvoker (Updater. Update ));
With this processing, multithreading should be thread-safe for user interface operations.

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.