windows8開發-使用背景工作執行緒與更新UI

來源:互聯網
上載者:User

使用TheadPool以及CoreDispatcher可以實現使用背景工作執行緒與更新UI的功能。

其中CoreDispatcher應當在UI線程中擷取,並在背景工作執行緒的callback函數中使用;否則CoreDispatcher是擷取不到的。

不過win8中建議是使用Task來代替TheadPool,因為它提供了一些比較實用的介面,例如取消背景工作執行緒中的任務等。

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;// 封裝了HttpPostRequestusing MyProject.HttpPost;namespace HttpTest{    /// <summary>    /// 系統管理使用者登陸退出    /// </summary>    public class AccountServer    {        // 用於調起UI線程        private Window m_CurWindow;        private CoreDispatcher m_CurCoreDispatcher;        // 登陸操作處理完成        public EventHandler<LoginCompletedEventArgs> LoginCompleted;        #region Constructor        public AccountServer()        {            m_CurWindow = Window.Current;        }        #endregion        #region Public Methods        /// <summary>        /// 使用者登陸        /// </summary>        /// <param name="userName">使用者名稱</param>        /// <param name="pwd">使用者密碼</param>        public void Login(string userName, string pwd)        {                       m_CurCoreDispatcher = m_CurWindow.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)            {                NotifyLoginCompletedEvent(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 LoginCompletedEventArgs(true, account));                }                else                {                    NotifyLoginCompletedEvent(new LoginCompletedEventArgs(false));                }            }        }        private void NotifyLoginCompletedEvent(LoginCompletedEventArgs args)        {            m_CurCoreDispatcher.RunAsync(CoreDispatcherPriority.High,() =>                {                    if (LoginCompleted != null)                        LoginCompleted(this, args);                });        }        #endregion    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.