开发者

Timers and Forms not closing when method called

开发者 https://www.devze.com 2022-12-15 05:07 出处:网络
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 > tha

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 proble开发者_开发问答m 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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消