简体   繁体   English

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

[英]how to use timer effectively in c#

I have to show one form for about 5 sec and then I have to close that form and then show some other form once the new form is shown the timer has to stop. 我必须显示一个表单大约5秒钟,然后我必须关闭该表单,然后在显示新表单时必须停止计时器后再显示其他表单。

I have difficulty in doing this. 我很难做到这一点。

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();
}

try this code 试试这个代码

 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();
  }

Pull the declaration of Form5 outside of the functions, so it is a field. Form5的声明Form5函数之外,因此它是一个字段。 As it stands, each function has a different instance of this class. 就目前而言, 每个函数都有一个此类的不同实例。

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();
}

Note: You really should rename your form classes and the variables - f6 is meaningless. 注意:您确实应该重命名表单类和变量f6没有意义。 Call it what it is. 叫它什么。

try this 尝试这个

    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();
}

I see several potential problems. 我看到了几个潜在的问题。 First, you should setup your timer only once, perhaps at Form construction time, you don't need to set the interval and wire up an even handler every single time radioButton1's check state changes. 首先,您应该只设置一次计时器,也许在Form构造时,您不需要设置时间间隔,也不必在每次radioButton1的检查状态更改时连接一个偶数处理程序。

Inside of MyTimer_Tick, the first line should be calling MyTimer.Stop() (just call stop, you don't need to mess with Enabled, they do the same thing). 在MyTimer_Tick内部, 第一行应调用MyTimer.Stop()(只需停止调用,就不必与Enabled混为一谈,它们执行相同的操作)。

THEN you can display the MessageBox (which is modal and blocking), show Form6, hide f5, etc. 然后,您可以显示MessageBox(模态框和阻塞框),显示Form6,隐藏f5等。

Think of MessageBox.Show() as a really long running call. 可以将MessageBox.Show()视为一个长期运行的调用。 It doesn't return until the message box is dismissed (it can easily take more than 5 seconds or any arbitrary amount of time). 直到关闭消息框后,它才会返回(它可能很容易花费5秒钟以上或任意时间)。 While the MessageBox is up, timer tick events are still queuing up (because the line that stops the timer hasn't been executed yet). 在MessageBox启动时,计时器滴答事件仍在排队(因为尚未执行停止计时器的行)。 It would be worth looking up the documentation for MessageBox.Show() and reading about what a modal dialog is and how its different from the alternative. 值得查阅MessageBox.Show()的文档,并了解什么是模式对话框以及它与替代对话框的不同之处。

And try and clean up the names as other's have pointed out. 并尝试清理其他人指出的名称。

I think You're making it to difficult. 我认为您正在变得困难。 If I understood You correctly... 如果我正确理解你...

Just add a timer to form5, set it's properties Enabled = true; 只需向form5添加一个计时器,将其属性设置为Enabled = true; and Interavl = 1000; Interavl = 1000; (1000 miliseconds or 1 second). (1000毫秒或1秒)。 and just add timer tick event handler to your form5 timer like 只需将计时器滴答事件处理程序添加到您的form5计时器中即可

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

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

_start and _seconds should be initialized like form class private fields, or properties before event handler. _start_seconds应该像表单类私有字段或事件处理程序之前的属性一样进行初始化。 This code works fine for me, and it closes form5 after 5 seconds it was shown. 这段代码对我来说很好用,并且在显示5秒钟后关闭了form5。 If you want to make this more flexible, for example if you want to set how many seconds form5 should be shown U can, for example, reload form5 constructor like ... 如果您想使其更加灵活,例如,如果您想要设置U应当显示form5多少秒,例如,可以重新加载form5构造函数,例如...

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

and in form1, when you create form5 pass number of seconds U want to show form5 as parameter: 在form1中,当您创建form5时,传递的秒数U要显示form5作为参数:

Form5 f5 = new Form5(5);

also I think it would be better to create new instance of form 5 dirrectly in event handler 我也认为最好在事件处理程序中直接创建表单5的新实例

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{

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

If U want to show some another more form after form5 close, just show it before form5 close in timer tick event handler: 如果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