C # using anonymous delegation to simplify thread and controle. Invoke writing

Source: Internet
Author: User

1. Thread
C # in the program, if you want to create a new thread, you usually need to create another method. If you want to pass parameters, the steps are troublesome,
Reference 1
Thread othread = new thread (New threadstart (threadmethod ));
Private void threadmethod (){
..
};

If you want to pass parameters to the thread

Public static void mystaticparamthreadmethod (Object OBJ ){
Console. writeline (OBJ );
}

Thread thread = new thread (mystaticparamthreadmethod );
Thread. Start (OBJ );

Of course, there are other writing methods. For more information, see reference 1.

However, if you use an anonymous class, it will be simpler.

Thread othread = new thread (delegate ()
{
....
});
Othread. Start ();

2. Control. Invoke

In C #, if you call multiple threads to update the UI attributes, an exception is reported: Cross-thread operation not valid.

The general solution is to declare a delegate and call it through the delegate. For details, refer to 2.

Private delegate void flushclient (); // proxy

Private void threadfunction ()
{
If (textbox1.invokerequired) // wait for Asynchronization
{
Flushclient fc = new flushclient (threadfunction );
This. Invoke (FC); // call the refresh method through the proxy
Return;
}
Textbox1.text = datetime. Now. tostring ();
}

Here, we need to declare a delegate first, which is quite troublesome, because we always need to write more delegates, but these delegates are a temporary transition. After being changed to an anonymous delegate, we do not need to declare those delegates.

Private void threadfunction ()
{
If (textbox1.invokerequired) // wait for Asynchronization
{
This. Invoke (New methodinvoker (delegate ()
{
Threadfunction ();
}));
Return;
}
Textbox1.text = datetime. Now. tostring ();
}

The amount of code is not reduced, but you do not have to declare the temporary delegate.

Reference 1: http://developer.51cto.com/art/200908/141590.htm
Reference 2: http://www.cnblogs.com/zhaotiantang/archive/2009/03/17/1414135.html

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.