1

I'm attempting to add a countdown to a data grid view which goes from 2 minutes to 0. When the button is pressed it should add a new row with a token and a 2 minute countdown. When the 2 minute countdown reaches 0 that row should be deleted. I have the token already set up and at the moment instead of a countdown I'm using time in 2 minutes. The main thing I want to achieve is deleting the token after 2 minutes which is when the token expires.

Here's my current code:

        //Add Token To Grid
        int row = 0;
        TokenGrid.Rows.Add();
        row = TokenGrid.Rows.Count - 2;
        TokenGrid["CaptchaToken", row].Value = CaptchaWeb.Document.GetElementById("gcaptcha").GetAttribute("value");
        //Time Left
        TokenGrid["ExpiryTime", row].Value = DateTime.Now.AddMinutes(2).ToLongTimeString();
Andre
  • 41
  • 1
  • 8

2 Answers2

0

In timer's timeelapsed() event, compare the value ExpiryTime of each row with current time. If ExpiryTime < current time, delete the row.

Ali Vojdanian
  • 2,067
  • 2
  • 31
  • 47
VDN
  • 717
  • 4
  • 12
0

Implement a timer (see How do I create a timer in WPF?):

const int MAX_DURATION = 120;
System.Windows.Threading.DispatcherTimer dispatcherTimer;

// In the OnClick
DateTime timerStart = DateTime.Now;
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
EventHandler handler = new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Tick += handler;
dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
dispatcherTimer.Start();

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
     // Display seconds 
     var currentValue = DateTime.Now - timerStart; 
     TokenGrid["ExpiryTime", row].Value = currentValue.Seconds.ToString();

     // When the MAX_DURATION (2 minutes) is reached, stop the timer
     if (currentValue >= MAX_DURATION) {
         dispatcherTimer.Tick -= handler;
         dispatcherTimer.Stop();
         TokenGrid.Rows.RemoveAt(row);
     }
}
Community
  • 1
  • 1
wake-0
  • 3,918
  • 5
  • 28
  • 45
  • I'm using Winforms not WPF. I can't find the 'System.Windows.Threading' name space and don't really know where to put the first part of the code. – Andre Jan 04 '17 at 21:43