Development in Windows 8-use a working thread and update the UI

Source: Internet
Author: User

Theadpool and coredispatcher can be used to implement the functions of working threads and updating the UI.

Coredispatcher should be obtained in the UI thread and used in the callback function of the working thread; otherwise, coredispatcher cannot be obtained.

However, in Win8, we recommend that you use task instead of theadpool because it provides some useful interfaces, such as canceling tasks in the working thread.

Using system; using system. collections. generic; using system. io; using Windows. UI. XAML; using Windows. system. threading; using system. threading. tasks; using Windows. UI. core; using Windows. foundation; // encapsulate httppostrequestusing myproject. httppost; namespace httptest {/// <summary> // manage user login and exit /// </Summary> public class accountserver {// used to call the UI thread private window m_curwindow; private coredispatcher m_curcored Ispatcher; // After the login operation is processed, public eventhandler <logincompletedeventargs> logincompleted; # region constructor public accountserver () {m_curwindow = Window. current ;} # endregion # Region Public Methods /// <summary> // User Logon /// </Summary> /// <Param name = "username"> User Name </param> /// <Param name = "PWD"> User Password </param> Public void login (string username, string PWD) {m_curcoredispatcher = m_current?dispatcher; Threadpool. runasync (asyncrequestlogin);} # region private methods private httppostrequest httppostrequestinstance {get {If (null = m_httppostrequest) {m_httppostrequest = new httppostrequest ();} return m_httppostrequest ;}} private void asyncrequestlogin (object state) {httppostrequestinstance. asyncrequest (loginurl, httppostparam. prepareloginparams (m_username, m_pwd), asynclogincallback );} Private void asynclogincallback (string MSG) {If (string. empty = MSG) {policylogincompletedevent (New logincompletedeventargs (false, false);} else {httppostresult res = new httppostresult (MSG); long error = res. geterrorcode (); If (error = httppostresult. noneerror) {account Account = res. getaccount (); account. islogging = true; account. password = m_pwd; notifylogincompletedevent (New logincomp Letedeventargs (true, account);} else {policylogincompletedevent (New logincompletedeventargs (false);} private void policylogincompletedevent (incluargs) {principal (coredispatcherpriority. high, () => {If (logincompleted! = NULL) logincompleted (this, argS) ;};## endregion }}

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.