1

I have a windows form application with time input(in minutes) which fires a GUI application after the timer elapses. Initially I take the input from the user and set the time. Say, the user enters 45 mins. After 45 mins, my other GUI application is launched. Currently I'm using this:

Timer MyTimer = new Timer();

private void Form1_Load(object sender, EventArgs e)
{
    MyTimer.Interval = 45mins // Input from user
    MyTimer.Tick += new EventHandler(MyTimer_Tick);
    MyTimer.Start();
}

private void MyTimer_Tick(object sender, EventArgs e)
{
    //pop my GUI application
}

so now, my question is, how can i extended the timer? Suppose while counting down in the 20th Minute, the user wishes to extend 15mins of the timer, i take the input as 15 from the user and after that, the timer should add this 15 mins to the existing time and fire the GUI app after 35mins. i.e, it should count from 35mins.In total after the time elapses, it would have been 50mins. How can I achieve this?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Pavan
  • 79
  • 11
  • thats just a pseudo code i wrote there.. my main objective is to extend it..how do i do that? – Pavan Sep 22 '15 at 16:16
  • Use a 1 minute timer, increment a variable until you reach 45. Add to that variable for the 15 extra minutes. – LarsTech Sep 22 '15 at 16:16
  • cant i achieve this in the timer api? and how do i display the timer count in the form? the real time timer – Pavan Sep 22 '15 at 16:18
  • There is no real-time timer. You can either use the forms timer or (preferably) a System.Timers.Timer. – Thorsten Dittmar Sep 22 '15 at 16:20

2 Answers2

2

Actually setting the timer to 1 second is just fine. there will be no performance hit. just keep track of the DateTime when it started, then you can use the tick event to display the elapsed time and check if that duration is greater than what the user wants

private DateTime timerStart;
private TimeSpan duration;
private void Form1_Load(object sender, EventArgs e)
{
    Timer MyTimer = new Timer();
    MyTimer.Interval = 1000; // tick at one second to update the UI
    MyTimer.Tick += new EventHandler(MyTimer_Tick);
    duration = whatever...// Input from user
    timerStart = DateTime.Now;
    MyTimer.Start();
}

private void changeTimer(TimeSpan newValue) {
     duration = newValue;
}

private void MyTimer_Tick(object sender, EventArgs e)
{
    TimeSpan alreadyElapsed = DateTime.Now.Subtract(timerStart);
    // update the UI here using the alreadyElapsed TimeSpan
    if(alreadyElapsed > duration) 
    {
    //pop my GUI application
    }
}
x4rf41
  • 5,184
  • 2
  • 22
  • 33
  • Great! thanks for your explanation!! :). One doubt: can the duration(new value) variable be set while this program(timer) is running? @x4rf41 – Pavan Sep 22 '15 at 16:30
  • @ThorstenDittmar the way i set the duration is completely thread safe afaik – x4rf41 Sep 22 '15 at 18:15
  • Unable to tell from your code. The `changeTimer` method could still be called from another thread. – Thorsten Dittmar Sep 22 '15 at 18:29
  • but thats what im saying, if i understood the answer from here correctly (http://stackoverflow.com/questions/2353014/are-c-sharp-structs-thread-safe) then even if changeTimer() is called from a different thread it should be safe, but maybe im wrong. doesnt matter anyway since he said its a user change and that means ui -> ui. edit: wrong link – x4rf41 Sep 22 '15 at 18:32
1

That's easy to implement if you set your timer to a one second/minute interval and another variable to the number of seconds/minutes.

Decrease the variable value on each timer tick. Add to that variable if you need to expand the interval. If the variable value is 0,launch the other application.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139