private void buttonConnect_Click(object sender, EventArgs e) { //If button clicking will trigger a time-consuming procedure, a thread must be started to handle it. //And after openning the thread, no operation depending on the result of the thread running should be included in the function m_xmppCH = new XMPPCommandsHandler(this, this.labelSatusSign, this.listBoxEvent); Thread connectXMPPThread = new Thread( unused => m_xmppCH.Connnect(textBoxUsername.Text, textBoxPassword.Text, textBoxServer.Text, "") ); connectXMPPThread.Start(); }
public int Logout() { try { if (m_account != null) { LogHelper.Debug("Try Log out"); //note these two threads below may call this logout, so after they are done, check m_account != null if(m_connectXMPPThread != null) m_connectXMPPThread.Join(); if (m_validateThread != null) m_validateThread.Join(); if (m_presenceInformer != null) { m_presenceInformer.RequestStopPing(); m_presenceInformer.Vanish(); m_presenceInformer = null; } if (m_syncRequestHandler != null) { m_syncRequestHandler.StopSending(); m_syncRequestHandler.Vanish(); m_syncRequestHandler = null; } //should be disconnected after m_presenceInformer and m_syncRequestHandler stopped if (m_xmppCH != null && m_xmppCH.IsOnline) { m_xmppCH.Disconnect(); m_xmppCH = null; } //set member variable that might be needed by other variables at last if (m_account != null) { string token = m_account.Token; m_account = null; m_user = null; m_isAuthorized = false; //once m_isAuthorized is false, the account is offline MessageBox.Show("Now offline!"); string[] paraName = new string[] { "user_id", "token", "client_version" }; string[] paraValue = new string[] { m_account.Id, token, ClientInfo.VERSION }; string xmlResponse = RestComm.SendRestRequest(RestComm.URL_ACCOUNT_LOGOUT, paraName, paraValue); if (xmlResponse != null) { LogHelper.Debug("Log out successfully"); return (int)Status.SUCCESS; } else { MessageBox.Show("No account found or server's dead!"); LogHelper.Debug("Log out failure: No account found"); //m_account.Status = "Offline"; //m_user.Status = "Offline"; return (int)Status.FAILURE; } } } MessageBox.Show("Already offline!"); LogHelper.Debug("Log out failure: Already offline"); return (int)Status.FAILURE; } catch (Exception ex) { LogHelper.Error(ex); return (int)Status.EXCEPTION; } }
public void LoginAndValidate() { if (!m_isAuthorized) { int loginStatus = Login(); if (m_isAuthorized && loginStatus == (int)Status.SUCCESS) { //newly authorized account needs validation //Validate require connecting to the xmpp server m_xmppCH = new XMPPCommandsHandler(m_qbClient, m_labelStatus, m_eventList); m_xmppCH.Auth = this; if (m_xmppCH != null && !m_xmppCH.IsOnline) { m_connectXMPPThread = new Thread( unused => m_xmppCH.Connnect("clientdemo", "secret", "yangzi-lius-macbook-pro.local", m_account.Token) ); m_connectXMPPThread.Start(); } m_validateThread = new Thread(ValidateCompanyMarker); m_validateThread.Start(); } else { LogHelper.Debug("Log in failed."); MessageBox.Show("Log in failed."); } } else { LogHelper.Debug("One user is online. Make it log out if you can."); MessageBox.Show("One user is online. Make it log out if you can."); } }