当我从一个表单检查到另一个表单时,如何停止计时器 [英] How can I stop my timer when I checked from one form to other
问题描述
我需要一个代码,当我从一个表单检查到另一个表格时我可以停止计时器,并且当新的值被赋予表单时,计时器应该更新时间。
我尝试过:
private void timer1_Tick_1(object sender,EventArgs e)
{if(textBox1.Enabled = true)
{
if(timer2.Enabled = true)
{
this.progressBar1.Increment(1);
this.progressBar2.Increment(0);
}
else if(textBox1.Enabled = false)
{
if(timer2.Enabled = true)
{
this.progressBar1.Increment(0);
this.progressBar2.Increment(1);
}
}
DateTime now = DateTime.Now;
label1.Text = now.ToString(dd-MM-yyyy hh:mm:sszzz);
// label1.Text = DateTime.Now.ToString(dd-MM-yyyy hh:mm:ss);
}}
I need a code were I can stop timer when I check from one form to other and when ever the new value is given to the form the timer should update the time.
What I have tried:
private void timer1_Tick_1(object sender, EventArgs e)
{if (textBox1.Enabled = true)
{
if (timer2.Enabled = true)
{
this.progressBar1.Increment(1);
this.progressBar2.Increment(0);
}
else if (textBox1.Enabled = false)
{
if (timer2.Enabled = true)
{
this.progressBar1.Increment(0);
this.progressBar2.Increment(1);
}
}
DateTime now = DateTime.Now;
label1.Text = now.ToString("dd-MM-yyyy hh:mm:sszzz");
// label1.Text = DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss");
} }
推荐答案
您是否尝试过可以轻松访问静态计时器?
Have you tried maybe a static timer accesible easily?
public class MyCommon
{
private static DispatcherTimer myTimer;
public static void StartNewTimer()
{
myTimer = new DispatcherTimer
{
Interval = new TimeSpan(0, 0, 1)
};
myTimer.Tick += (s, a) =>
{
// I am ticking here
};
myTimer.Start();
}
public static void StopTimer()
{
myTimer.Stop();
}
}
public class MyA
{
public MyA()
{
StartFromA();
}
public void StartFromA()
{
MyCommon.StartNewTimer();
}
}
public class MyB
{
public MyB()
{
StartFromA();
}
public void StartFromB()
{
MyCommon.StopTimer();
}
}
这篇关于当我从一个表单检查到另一个表单时,如何停止计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!