Ejemplo n.º 1
0
        void connection_TimeRemainingChanged(ConnectionManager conn, TimeEvents e)
        {
            if (InvokeRequired)
            {
                Invoke(new Action<ConnectionManager, TimeEvents>(connection_TimeRemainingChanged), conn, e);
                return;
            }

            if (e.TimeRemanining.TotalSeconds <= 1)
            {
                lblStatus.Text = "Logging you off...";
                lblStatus.ForeColor = Color.Red;
                lblStatus.Visible = true;
            }
            else
            {
                lblStatus.Text = "Time remaining: " + e.TimeRemanining.ToString("g");
                lblStatus.ForeColor = Color.Green;
                lblStatus.Visible = true;
            }
        }
        /*
         * Send a keepalive message to the server whenever the timer tells it to.
         * Triggers an event once remaining time has been determined.
         */
        private void Keepalive(object sender, System.Timers.ElapsedEventArgs e)
        {
            string postString = "terminalId=" + TerminalId + "&timestamp=" + GetTimestamp() + "&token=" + LoginToken;
            string response = GetResponse(KeepAliveUrl, postString);

            Dictionary<string, object> parsed = ParseJSON(response);
            if (parsed.ContainsKey("timeRemaining"))
            {
                int seconds = (int)parsed["timeRemaining"];
                if (seconds <= 0)
                {
                    StopKeepalive();

                    Thread.Sleep(10000);

                    // Force log off
                    ExitWindowsEx(4, 0);
                }
                else if (TimeRemainingChanged != null)
                {
                    TimeEvents time = new TimeEvents();
                    time.TimeRemanining = new TimeSpan(0, 0, seconds);
                    TimeRemainingChanged(this, time);
                }
            }
        }