C# Can';停止计时器
我创建了Forms.Timer的子类,无法停止此计时器C# Can';停止计时器,c#,timer,C#,Timer,我创建了Forms.Timer的子类,无法停止此计时器 public class Clock : Timer { private int startTime; private PictureBox one; private PictureBox two; private PictureBox three; public Clock(Size _size) : base() { startTime = 0; initialCompone
public class Clock : Timer
{
private int startTime;
private PictureBox one;
private PictureBox two;
private PictureBox three;
public Clock(Size _size) : base()
{ startTime = 0; initialComponent(_size); }
public Clock(int start_time, Size _size) : base()
{ startTime = start_time; initialComponent(_size); }
public Point Location
{
get { return three.Location; }
set
{
three.Location = value;
two.Location = new Point(value.X + three.Width, value.Y);
one.Location = new Point(value.X + three.Width + two.Width, value.Y);
}
}
private void initialComponent(Size _size)
{
......
}
public void Show()
{
int bits = startTime % 10;
int ten = (startTime % 100) / 10;
int hundred = (startTime % 1000) / 100;
one.Image = ImageLauncher.loadImage(bits, ImageLauncher.ImageType.Timer);
two.Image = ImageLauncher.loadImage(ten, ImageLauncher.ImageType.Timer);
three.Image = ImageLauncher.loadImage(hundred, ImageLauncher.ImageType.Timer);
}
protected override void OnTick(EventArgs e)
{
startTime++;
if (startTime > 999 || startTime < 0) return;
this.Show();
}
}
开始:
if (!clock.Enabled) clock.Start();
带停止的方法:
private void gameOver(bool is_win)
{
clock.Stop();
showAll();
string winface = is_win ? "cool" : "sad";
ShortCutStart.Image = ImageLauncher.loadImage(winface, ImageLauncher.ImageType.Face);
ShortCutStart.Name = winface;
}
您是否尝试在
gameOver()
中使用这一行,clock.Enabled=false
(在上面插入此showAll();
。尝试后,该方法中有关时钟的任何进程都将运行,onTick事件不会停止,并且在事件中启用的值将更改为true。如果(!clock.enabled)clock.Start()在这里,您正在启动未启用的时钟。这是您想要的吗?这不应该是如果(clock.Enabled)clock.Start();
。也就是说,只有在启用时才启动时钟吗?这不应该是如果(clock.Enabled)clock.Start();这将是多余的。应该工作。showAll()中有什么内容?您是否尝试过gameOver()
,clock.Enabled=false;
(在上面插入此showAll();
。尝试过之后,该方法中有关时钟的任何进程都将运行,onTick事件不会停止,并且在事件中启用的值将更改为true。如果(!clock.Enabled)clock.Start()
在这里,您正在启动未启用的时钟。这是您想要的吗?这不应该是如果(clock.Enabled)clock.Start();
。也就是说,只有启用了才启动时钟吗?如果(clock.Enabled)clock.Start();这不应该是多余的。应该工作。showAll()中有什么内容;?
private void gameOver(bool is_win)
{
clock.Stop();
showAll();
string winface = is_win ? "cool" : "sad";
ShortCutStart.Image = ImageLauncher.loadImage(winface, ImageLauncher.ImageType.Face);
ShortCutStart.Name = winface;
}