Improve UI response speed

Source: Internet
Author: User
Constraints on Windows Forms architecture:
No member of the UI can be called by other threads (worker threads) other than the UI thread.

For example:

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Threading;

Namespace Test
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}

Public Delegate int onadd (int I, Int J );

Int add (int m, int N)
{
Int sum = m + N;
Button1.text = sum. tostring ();

Return sum;
}

Private void button#click (Object sender, eventargs E)
{
Onadd proc = add;
System. iasyncresult async = Proc. begininvoke (1, 1, null, null );
// The following two lines are waiting for proc to complete.
// Reflection (assuming intadd takes some time to complete), you need to comment them out.
Int sum = Proc. endinvoke (async );
Console. writeline (SUM );
}
}
}

The asynchronous delegate call accesses button1.text through the thread in the thread pool. An error occurred!

Correct UI update method:

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Threading;

Namespace Test
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}

Public Delegate int onadd (int I, Int J );
Public Delegate void onupdateui (Object sender, string text );

Int add (int m, int N)
{
Int sum = m + N;
// Button1.text = sum. tostring ();
Object [] plist = {This, Sum. tostring ()};
Button1.begininvoke (New onupdateui (updateui), plist );

Return sum;
}

Private void updateui (Object sender, string text)
{
Button1.text = text;
}

Private void button#click (Object sender, eventargs E)
{
Onadd proc = add;
System. iasyncresult async = Proc. begininvoke (1, 1, null, null );
// The following two lines are waiting for proc to complete.
// Reflection (assuming intadd takes some time to complete), you need to comment them out.
Int sum = Proc. endinvoke (async );
Console. writeline (SUM );
}
}
}

Http://www.microsoft.com/china/MSDN/library/enterprisedevelopment/softwaredev/misMultithreading.mspx? MFR = true

To be continued...

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.