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 }}