tags:

views:

139

answers:

2

I have three seperate timers that call a method in each of their _Tick. This method works fine, as intended, but within it is an if statement which checks to see if two values are either < or > than a number:

if ((x < y) || (x > z))
{

}

and within this statement, I want to stop those three times, show a message box and dispose the form. This is the code I'm using:

if ((x < yArray[0]) || (x > yArray[1]))
{
   frmFooBar barFoo = new frmFooBar();
   barFoo.tmrOne.Stop();
   barFoo.tmrTwo.Stop();
   barFoo.tmrThree.Stop();
   MessageBox.Show(GlobalVariables.aVariable+ " is dead.");
   barFoo.Dispose();
}

The conditional works fine, as when either statement is true, I'll get a MessageBox popup every tick. The problem I'm having is that none of those form functions are working. tmrOne,Two keep running, and the form does not close.

I'm still new to C# so maybe my problem is obvious, but any help would be great! Thank you.

+3  A: 

You're doing this in your method:

frmFooBar barFoo = new frmFooBar(); 
barFoo.tmrOne.Stop(); 
barFoo.tmrTwo.Stop(); 

This creates a new instance of frmFooBar, and has no effect on the instance that's already running.

If this is happening within the tick event of a timer on your form, you'll want to use "this", since that will be the currently running form:

this.tmrOne.Stop();
this.tmrTwo.Stop();

That way, you're stopping the timers on the currently running form.

Reed Copsey
This makes perfect sense, and works! Thank you!
BlindMatoya
A: 

to Close the form you need to use form.Close(); you can use this.Close(); if you want to close the form you'r in. to stop the timer use the Timer.Enabled = false; Hope it help Amit

Mazki516
oh, there is a chance you are using global verbs? cause then you need to pass the form with the timers to the form you are calling this method from..
Mazki516
if not, just call directly to the timers and the close method without the 'FormName'.Timer.Stop()
Mazki516