execute a code after every 5 min in C#.

execute a code after every 5 min in C# (.net)

Page 1 of 1

6 Replies - 43982 Views - Last Post: 28 October 2010 - 05:50 AM Rate Topic: -----

#1 shamshersingh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-October 10

execute a code after every 5 min in C#.

Posted 26 October 2010 - 11:11 PM

Dear All,

I want to execute a code after every 5 min.
I am trying to develop the application in .net and using the C# as a backend language.

My basic requirement is a C# code which executes after every 5min which i click a start button on my .net form.

Thanks&Regards
Shamsher Singh
Is This A Good Question/Topic? 0
  • +

Replies To: execute a code after every 5 min in C#.

#2 motcom   User is offline

  • D.I.C Lover
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: execute a code after every 5 min in C#.

Posted 26 October 2010 - 11:57 PM

You would use a Timer to do that...
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: execute a code after every 5 min in C#.

Posted 27 October 2010 - 06:44 AM

The simplest timer to use (and easiest to use with Windows Forms) is the System.Windows.Forms.Timer.

There is an example of use in the page I linked.
Was This Post Helpful? 0
  • +
  • -

#4 cmatija   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-October 10

Re: execute a code after every 5 min in C#.

Posted 27 October 2010 - 11:15 AM

Add the timer. Set the interval to 5min (300s(30000ms))
Use the activated event on the form to start the timer when the form activates.
Use the "Tick" event to excecute the code you want.


A simplest example would be:

1. Make a textbox (textBox1)
2. Make a timer (timer1)

private void Form1_Activated(object sender, EventArgs e)
        {
            timer1.Start();
        }

private void timer1_Tick(object sender, EventArgs e)
        {
            textBox1.Text = "Hello World";
            timer1.Stop();
        }



EDIT: It excecutes every 5min just that you don't see it. (It writes "Hello World" but it's already written)

O.K. Maybe not the best example but you surely get the point.

This post has been edited by cmatija: 27 October 2010 - 11:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: execute a code after every 5 min in C#.

Posted 27 October 2010 - 11:25 AM

View Postcmatija, on 27 October 2010 - 02:15 PM, said:

Add the timer. Set the interval to 5min (300s(30000ms))


30000ms is 30 seconds. It should be 300000ms is 300 seconds(5 minutes).
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: execute a code after every 5 min in C#.

Posted 27 October 2010 - 12:11 PM

If I'm using milliseconds to represent time, and I'm representing more than a few seconds, I prefer to do it in a way that isn't very ambiguous:

(int)TimeSpan.FromMinutes(5).TotalMilliseconds


It's a little more to type, but it's quite obvious how long the interval is at a glance.
Was This Post Helpful? 4
  • +
  • -

#7 cmatija   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-October 10

Re: execute a code after every 5 min in C#.

Posted 28 October 2010 - 05:50 AM

View PostinsertAlias, on 27 October 2010 - 11:11 AM, said:

If I'm using milliseconds to represent time, and I'm representing more than a few seconds, I prefer to do it in a way that isn't very ambiguous:

(int)TimeSpan.FromMinutes(5).TotalMilliseconds


It's a little more to type, but it's quite obvious how long the interval is at a glance.


You're right...
And i'm sorry for the mistake (30000ms) but the point is still the same...

This post has been edited by cmatija: 28 October 2010 - 05:50 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1