Ron
Ron

Reputation: 11

Timer + display messages + Different time intervals

How to display some messages on a C# form application with different time intervals with buttons?

Something like:

private void button1_Click(object sender, EventArgs e)
{
      label1.Text = "string1";

      [wait 3 seconds]

      label1.Text = "string2";

      [wait 5 sec]

      label1.text="string 3";

      [end]
}

Upvotes: 1

Views: 4365

Answers (5)

CodingBarfield
CodingBarfield

Reputation: 3398

Use a Timer with an interval of X milliseconds and update the UI each Timer Tick. Keep track of the number of Timer Ticks received so you'll know which string to use. After each update has been processed stop the Timer.

Other solutions posted might be wiser, but this one is pretty simple.

Form1 contains a simple Label called Label1 and a button called Button1

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        t.Interval = 100;
        t.Tick += new EventHandler(t_Tick);
        t.Start();
    }

    Timer t = new Timer();
    int counter = 0;

    private void Button1_Click(object sender, EventArgs e)
    {
        t.Start();
    }

    void t_Tick(object sender, EventArgs e)
    {
        try
        {
            t.Enabled = false; //Disable timer so we don't start t_Tick when t_Tick is still runnnig

            if (counter == 0)
            {
                label1.Text = "string1";
                t.Interval = 3000;
            }
            if (counter == 1)
            {
                label1.Text = "string2";
                t.Interval = 5000;
            }
            if (counter == 2)
            {
                label1.Text = "string3";
                t.Stop(); //Stop timer
            }
            else
            {
                t.Enabled = true; //Resume timer
            }

            counter++;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Never throw exception from timer..." + ex.Message);
        }

    }

Upvotes: 1

Ron
Ron

Reputation: 11

Thank you sooo much.

Oh i love stakoverflow.com I did something like this.:) Thank you all gentle mens :) Please comment how did i do? I actually wanted it all happen in click of button.

public partial class mainForm : Form { public mainForm()

Timer myTimer = new Timer();

private void button1_Click(object sender, EventArgs e)

    {
       myTimer.Tick += new EventHandler(myTimer_Tick);
       myTimer.Interval = 2000; 
       myTimer.Start();
    }

int counter=0;

    void myTimer_Tick(object sender, EventArgs e)
    {

        if (counter == 0)
        {
            label4.Text = "string1";
            myTimer.Interval = 2000;
        }
        if (counter == 1)
        {
            label4.Text = "string2";
            myTimer.Interval = 2000;
        }
        if (counter == 2)
        {
            label4.Text = "string3";
            myTimer.Stop(); 
        }
        else
        {
            myTimer.Enabled = true; 
        }
        counter++;
      } }

I made it all work with valuable examples you all provided . i put it all together and got it working as i wanted. Once again Thank you all :)

Upvotes: 0

fardjad
fardjad

Reputation: 20424

You can create a new thread, change the label text, sleep that thread and so on so forth:

using System.Threading;

// Somewhere in your Form, for example in Form_Load event:
new Thread(new ThreadStart(delegate {
    var d = new setLabelTextDelegate(setLabelText);
    label1.Invoke(d, new object[] { "string 1" });
    Thread.Sleep(3000); // sleep 3 seconds
    label1.Invoke(d, new object[] { "string 2" });
    Thread.Sleep(5000); // sleep 5 seconds
    label1.Invoke(d, new object[] { "string 3" });
})).Start();

private delegate void setLabelTextDelegate(string text);
private void setLabelText(string text)
{
    this.label1.Text = text;
}

Upvotes: 2

user1120193
user1120193

Reputation: 240

private void button1_Click(object sender, EventArgs e)
{

      label1.Text = "string1";
      System.Threading.Thread.Sleep(3*1000);
        label1.Text = "string2";
       System.Threading.Thread.Sleep(5*1000);
        label1.text="string 3";
}

Upvotes: 0

Ilia G
Ilia G

Reputation: 10221

To do exactly as your pseudo code suggest simply use Thread.Sleep() in place of your [wait x] lines. Note that it will likely make UI unresponsive for the duration of waiting.

Alternatively you can create a thread that does the same thing but doesn't block the UI thread. The only issue there is that you have to define delegate in UI thread otherwise it wont work.

private void button1_Click(object sender, EventArgs e)
{
    ThreadPool.QueueUserWorkItem(delegate()
    {
        label1.Text = "string1";
        Thread.Sleep(3000);
        label1.Text = "string2";
        Thread.Sleep(5000);
        //etc...
    });
}

Upvotes: 0

Related Questions