繁体   English   中英

如何在C#中有效使用计时器

[英]how to use timer effectively in c#

我必须显示一个表单大约5秒钟,然后我必须关闭该表单,然后在显示新表单时必须停止计时器后再显示其他表单。

我很难做到这一点。

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    Form5 f5 = new Form5();
    f5.Show();
    f5.label7.Text = label6.Text;

    MyTimer.Interval = 5000; // 0.5 mins
    MyTimer.Tick += new EventHandler(MyTimer_Tick);
    MyTimer.Start();
}

private void MyTimer_Tick(object sender, EventArgs e)
{
    MessageBox.Show("All The Best for Your Test and Your Time Starts Now.");
    Form6 f6 = new Form6();
    f6.Show();
    MyTimer.Enabled = false;

    Form5 f5 = new Form5();
    f5.Hide();
}

试试这个代码

 Form5 f5 = new Form5();
 private void radioButton1_CheckedChanged(object sender, EventArgs e)
 {
    f5.Show();
    f5.label7.Text = label6.Text;

    MyTimer.Interval = 5000; // 0.5 mins
    MyTimer.Tick += new EventHandler(MyTimer_Tick);
    MyTimer.Start();
  }

  private void MyTimer_Tick(object sender, EventArgs e)
  {
    MessageBox.Show("All The Best for Your Test and Your Time Starts Now.");
    Form6 f6 = new Form6();
    f6.Show();
    MyTimer.Enabled = false;
    MyTimer.stop();        

    f5.Hide();
  }

Form5的声明Form5函数之外,因此它是一个字段。 就目前而言, 每个函数都有一个此类的不同实例。

Form5 f5 = new Form5();

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    f5.Show();
    f5.label7.Text = label6.Text;

    MyTimer.Interval = 5000; // 0.5 mins
    MyTimer.Tick += new EventHandler(MyTimer_Tick);
    MyTimer.Start();
}

private void MyTimer_Tick(object sender, EventArgs e)
{
    MessageBox.Show("All The Best for Your Test and Your Time Starts Now.");
    Form6 f6 = new Form6();
    f6.Show();
    MyTimer.Enabled = false;
    MyTimer.Stop();

    f5.Hide();
}

注意:您确实应该重命名表单类和变量f6没有意义。 叫它什么。

尝试这个

    Form5 f5 = new Form5();  //declare form obj globally 

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{

    f5.Show();
    f5.label7.Text = label6.Text;

    MyTimer.Interval = 5000; // 0.5 mins
    MyTimer.Tick += new EventHandler(MyTimer_Tick);
    MyTimer.Start();
}

private void MyTimer_Tick(object sender, EventArgs e)
{
    MessageBox.Show("All The Best for Your Test and Your Time Starts Now.");
    Form6 f6 = new Form6();
    f6.Show();
    MyTimer.Enabled = false;


    f5.Hide();
}

我看到了几个潜在的问题。 首先,您应该只设置一次计时器,也许在Form构造时,您不需要设置时间间隔,也不必在每次radioButton1的检查状态更改时连接一个偶数处理程序。

在MyTimer_Tick内部, 第一行应调用MyTimer.Stop()(只需停止调用,就不必与Enabled混为一谈,它们执行相同的操作)。

然后,您可以显示MessageBox(模态框和阻塞框),显示Form6,隐藏f5等。

可以将MessageBox.Show()视为一个长期运行的调用。 直到关闭消息框后,它才会返回(它可能很容易花费5秒钟以上或任意时间)。 在MessageBox启动时,计时器滴答事件仍在排队(因为尚未执行停止计时器的行)。 值得查阅MessageBox.Show()的文档,并了解什么是模式对话框以及它与替代对话框的不同之处。

并尝试清理其他人指出的名称。

我认为您正在变得困难。 如果我正确理解你...

只需向form5添加一个计时器,将其属性设置为Enabled = true; Interavl = 1000; (1000毫秒或1秒)。 只需将计时器滴答事件处理程序添加到您的form5计时器中即可

private int _start = 0;
private int _seconds = 5;

private void timer1_Tick(object sender, EventArgs e)
{
        _start++;
        if(_start >= _seconds)
        {
             Close();
        }
}

_start_seconds应该像表单类私有字段或事件处理程序之前的属性一样进行初始化。 这段代码对我来说很好用,并且在显示5秒钟后关闭了form5。 如果您想使其更加灵活,例如,如果您想要设置U应当显示form5多少秒,例如,可以重新加载form5构造函数,例如...

public Form5(int seconds)
{
       InitializeComponent();
       _seconds = seconds;
}

在form1中,当您创建form5时,传递的秒数U要显示form5作为参数:

Form5 f5 = new Form5(5);

我也认为最好在事件处理程序中直接创建表单5的新实例

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{

    new Form5(10).Show(); // show form5 for 10 seconds
    ...
}

如果U想要在关闭form5之后显示其他表单,只需在计时器刻度事件处理程序中在form5关闭之前显示它:

private void timer1_Tick(object sender, EventArgs e)
    {
            _start++;
            if(_start >= _seconds)
            {
                 new Form2().Show();
                 Close();
            }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM