18

I want to add a timer rather than a countdown which automatically starts when the form loads. Starting time should be 45 minutes and once it ends, i.e. on reaching 0 minutes, the form should terminate with a message displayed. How can I do this?

Language: preferably C#.

Mike
  • 47,263
  • 29
  • 113
  • 177
knowledgehunter
  • 1,345
  • 4
  • 11
  • 10
  • 1
    Personally, I enjoy being able to help with homework. The key word being *help*. So why are we *doing* someone's homework without some reasonable attempt at showing us an effort? – IAbstract Feb 11 '10 at 20:19

3 Answers3

61

Bit more detail:

    private void Form1_Load(object sender, EventArgs e)
    {
        Timer MyTimer = new Timer();
        MyTimer.Interval = (45 * 60 * 1000); // 45 mins
        MyTimer.Tick += new EventHandler(MyTimer_Tick);
        MyTimer.Start();
    }

    private void MyTimer_Tick(object sender, EventArgs e)
    {
        MessageBox.Show("The form will now be closed.", "Time Elapsed");
        this.Close();
    }
Tim
  • 736
  • 7
  • 9
  • hey bro thanx a lot...it worked.... actually i m a fresher in prog so i would really appreciate if u explain me how it works.especially these two lines: MyTimer.Tick += new EventHandler(MyTimer_Tick); MyTimer.Start(); and why 1000??i.e in 45*60*1000.... and i want to display the time(countdown) display in a label or somthn like dat on the form... thanx – knowledgehunter Jul 17 '09 at 12:43
  • MyTimer.Tick += new EventHandler(MyTimer_Tick); The timer has an event called Tick when the set interval elapses. In this case, we are saying that we want to invoke the MyTimer_Tick method when the tick event occurs. MyTimer.Interval = (45 * 60 * 1000); The interval of the timer is in milliseconds and thus I used a calculation rather than just working out the answer and sticking in the value. I think this makes it a bit easier to understand. – Tim Jul 17 '09 at 12:59
  • thanx...i got it...bt wat about displaying the countdown at the form?? – knowledgehunter Jul 17 '09 at 13:27
  • If you want to display countdown then you have to use timer with higher granularity (eg. 1 second or 1 minute, depending on what you expect) and handle the logic inside it. – Filip Navara Aug 16 '09 at 17:59
3

Something like this in your form main. Double click the form in the visual editor to create the form load event.

 Timer Clock=new Timer();
 Clock.Interval=2700000; // not sure if this length of time will work 
 Clock.Start();
 Clock.Tick+=new EventHandler(Timer_Tick);

Then add an event handler to do something when the timer fires.

  public void Timer_Tick(object sender,EventArgs eArgs)
  {
    if(sender==Clock)
    {
      // do something here      
    }
  }
Maestro1024
  • 3,173
  • 8
  • 35
  • 52
-8

Download http://download.cnet.com/Free-Desktop-Timer/3000-2350_4-75415517.html

Then add a button or something on the form and inside its event, just open this app ie:

{

Process.Start(@"C:\Program Files (x86)\Free Desktop Timer\DesktopTimer");

}

  • 4
    This is a terrible solution... download a 3rd party Timer, pffft. Why wouldn't you use the standard Timer control? Are you the author of this component? – Jeremy Thompson Jun 24 '15 at 06:43
  • I'm a beginner.. But actually how is it possible to display the timer control during a runtime? like the third party timer... pls answer if u know.. – Sunil Jere Jun 24 '15 at 07:10
  • Private Sub timer1_Tick(sender As Object, e As EventArgs) Dim diff As New TimeSpan() diff = DateTime.Now.Subtract(startTime) lblTime.Text = diff.Hours & ":" & diff.Minutes.ToString("00") & ":" & diff.Seconds.ToString("00") lblTime.Invalidate() End Sub – Jeremy Thompson Jun 24 '15 at 07:39
  • In Visual studio winforms, C#? – Sunil Jere Jun 25 '15 at 07:22
  • 1
    Meh, you didn't even thank me for providing the VB.Net code. – Jeremy Thompson Jun 25 '15 at 07:24
  • Sorry for that cos I haven't tried VB ever... Thanks anyways ;) – Sunil Jere Jun 25 '15 at 10:39