//----------------------------------------------------------------------------
//
// Copyright © 2006-2011, Intel Corporation. All rights reserved.
//
// File: CountDownTimer.cs
//
//----------------------------------------------------------------------------
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using UCT.Utils;
namespace UCT.Forms
{
///
/// The timer control (count down timer for 15 min.)
///
public partial class CountDownTimer : UserControl
{
#region - Members -
private string time = String.Empty;
private long StartTime = 0;
public static bool consentTimer = false;
#endregion
#region - Constructors -
public CountDownTimer()
{
InitializeComponent();
}
#endregion
#region - Methods -
#region - Private Methods -
///
/// Convert the long string to time
///
///
private long ConvertSetTime()
{
long seconds = 0;
if (time.Trim().Substring(0, 1) == ":") // if leftmost colon, take it off
time = time.Substring(1);
string[] vals = time.Split(new char[] { ':', ' ', '.', '/' });
for (int i = 0; i < vals.Length; i++)
{
seconds += Convert.ToInt32(vals[i]) * (long)Math.Pow((double)60, (double)(vals.Length - (1 + i)));
}
return seconds;
}
///
/// Tick event (update the string time)
///
///
///
private void timer_Tick_1(object sender, EventArgs e)
{
StartTime--;
Invalidate();
lblTimer.Text = ConvertToTime(StartTime);
if (StartTime <= 0 && consentTimer)
{
consentTimer = false;
DialogResult result = new DialogResult();
result = AMT_SW_GUI.MessageManager.ShowMessage("User consent expired.To renew and restart the expiration timer, click on StartConsent button.", "Expired consent");
timer.Stop();
}
}
///
/// Convert the tick time to string
///
///
///
private string ConvertToTime(long tickCount)
{
// tickcount is in milliseconds, convert to minutes and seconds
long seconds = tickCount;
string val = (seconds / 60).ToString("00") + ":" + (seconds % 60).ToString("00");
return val;
}
#endregion
#region - Public Methods -
///
/// Set the timer time (e.g. for how long the timer will run)
///
///
public void SetTimer(uint Time)
{
string timeString = ConvertToTime(Time);
timer.Stop();
lblTimer.Text = timeString;
if (Time <= 0 && consentTimer)
{
consentTimer = false;
DialogResult result = new DialogResult();
result = AMT_SW_GUI.MessageManager.ShowMessage("User consent expired.To renew and restart the expiration timer, click on StartConsent button.", "Expired consent");
}
}
///
/// String the timer
///
///
public void StartTimer(string Time)
{
this.time = Time;
StartTime = ConvertSetTime();
timer.Enabled = true;
consentTimer = true;
timer.Start();
}
///
/// Cancle the consentTimer
///
public void CancleConsentTimer()
{
consentTimer = false;
}
#endregion
#endregion
}
}