The simplest way to solve the problem where C # performs time-consuming methods in the UI thread causes the interface to suspend animation

Source: Internet
Author: User
int sta =  my_connect.login (_user_name, _password);

As above, the login method through HTTP to the server to send a request, in the network condition is not good or the server is busy, because the delay can not return results caused the page suspended animation.

Solution:

int sta = await task.run (() =>
            {return
                my_connect.login (_user_name, _password);
            });

Awit keyword waits for task to complete

Task.run opened up a new thread

In this way, the UI thread is not blocked and solves the problem of suspended animation of the interface.

There are many ways to multithreading, but because. NET does not allow access to controls on non-UI threads, the above approach is most appropriate for situations where the UI needs to be updated after the long time-consuming task has finished executing.

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.