简体   繁体   中英

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.

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. 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. 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.

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).

THEN you can display the MessageBox (which is modal and blocking), show Form6, hide f5, etc.

Think of MessageBox.Show() as a really long running call. It doesn't return until the message box is dismissed (it can easily take more than 5 seconds or any arbitrary amount of time). While the MessageBox is up, timer tick events are still queuing up (because the line that stops the timer hasn't been executed yet). 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.

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; and Interavl = 1000; (1000 miliseconds or 1 second). and just add timer tick event handler to your form5 timer like

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. This code works fine for me, and it closes form5 after 5 seconds it was shown. 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 ...

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:

Form5 f5 = new Form5(5);

also I think it would be better to create new instance of form 5 dirrectly in event handler

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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