Reputation: 103
so, i want this: if specific time passed (for example 9 hours) from loading form, than i want to show messagebox said "9 hours passed". my code is this:
public partial class Form1 : Form
{
Stopwatch stopWatch = new Stopwatch();
public Form1()
{
InitializeComponent();
stopWatch.Start();
}
private void button1_Click(object sender, EventArgs e)
{
double sec = stopWatch.ElapsedMilliseconds / 1000;
double min = sec / 60;
double hour = min / 60;
if (hour == 9.00D)
{
stopWatch.Stop();
MessageBox.Show("passed: " + hour.ToString("0.00"));
}
}
}
and the problem is that i don't know where to write this part of code:
if (hour == 9.00D)
{
stopWatch.Stop();
MessageBox.Show("passed: " + hour.ToString("0.00"));
}
so, where i write this code? if you have better way of doing this, please show me.
Upvotes: 2
Views: 3570
Reputation: 54791
What people are not appreciating is that it is very unlikely that the double hours
will be exactly 9.00! Why not just ask your timer to fire once, after the time you want, 9 hours.
Timer timer;
public Form1()
{
InitializeComponent();
timer.Tick += timer_Tick;
timer.Interval = TimeSpan.FromHours(9).TotalMilliseconds;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
MessageBox.Show("9 hours passed");
}
Upvotes: 2
Reputation: 39132
In addition to using a Timer as outlined by the others, you can directly use the TotalHours() property of the TimeSpan returned by Stopwatch.Elapsed:
TimeSpan ts = stopWatch.Elapsed;
if (ts.TotalHours >= 9)
{
MessageBox.Show("passed: " + ts.TotalHours.ToString("0.00"));
}
Upvotes: 3
Reputation: 10153
Use Timer:
Timer regularly invokes code. Every several seconds or minutes, it executes a method. This is useful for monitoring the health of an important program, as with diagnostics. The System.Timers namespace proves useful.
see this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
stopWatch.Start();
tm.Interval = 1000;
tm.Enabled = true;
tm.Tick += new EventHandler(tm_Tick);
tm.Start();
}
void tm_Tick(object sender, EventArgs e)
{
double sec = stopWatch.ElapsedMilliseconds / 1000;
double min = sec / 60;
double hour = min / 60;
if (hour == 9.00D)
{
stopWatch.Stop();
MessageBox.Show("passed: " + hour.ToString("0.00"));
}
}
}
Upvotes: 0
Reputation: 7921
Try using Timer instead. Example from here
Timer timer;
public Form1()
{
InitializeComponent();
timer.Tick += new EventHandler(timer_Tick); // when timer ticks, timer_Tick will be called
timer.Interval = (1000) * (10); // Timer will tick every 10 seconds
timer.Enabled = true; // Enable the timer
timer.Start(); // Start the timer
}
void timer_Tick(object sender, EventArgs e)
{
double sec = stopWatch.ElapsedMilliseconds / 1000;
double min = sec / 60;
double hour = min / 60;
if (hour == 9.00D)
{
stopWatch.Stop();
MessageBox.Show("passed: " + hour.ToString("0.00"));
}
}
Upvotes: 0
Reputation: 32681
In order to do a specific task after a specific period of time System.Forms.Timer should be used (in case of windows forms). You can use its Elapsed event and in that you can implement your conditions.
Upvotes: 1