Murhaf Sousli
Murhaf Sousli

Reputation: 13296

How do I show text on a label for a specific time (like 3 seconds)?

I have a statusbar label and I want to show a text on my StatusBar Label for 3 seconds only

How can I do it without using threads?

public void InfoLabel(string value)
    {
        if (InvokeRequired)
        {
            this.Invoke(new Action<string>(InfoLabel), new object[] { value });
            return;
        }
        infoLabel.Text = value;
    }

Upvotes: 2

Views: 6989

Answers (4)

Hammi
Hammi

Reputation: 101

You need to define a function that you call each time you need to display your text, inside this function you define a timer, this timer is based on System.Windows.Forms.Timer, the only difference is that its modified to hold a stopTime parameter that represents the running duration, the only thing you need to do is to put your starting code(display text) inside the MyFunction function and to put the ending code(to stop displaying text) inside the Timer_Tick function, once you call MyFunction just specify how many seconds you want it to run in the function parameter.

 private void MyFunction(int durationInSeconds)
    {
        MyTimer timer = new MyTimer();
        timer.Tick += new EventHandler(Timer_Tick); 
        timer.Interval = (1000) * (1);  // Timer will tick every second, you can change it if you want
        timer.Enabled = true;
        timer.stopTime = System.DateTime.Now.AddSeconds(durationInSeconds);
        timer.Start();
        //put your starting code here
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        MyTimer timer = (MyTimer)sender;
        if (System.DateTime.Now >= timer.stopTime)
        {
            timer.Stop();
            //put your ending code here
        }
    }

the modified timer class

public class MyTimer : System.Windows.Forms.Timer
{
    public System.DateTime stopTime;
    public MyTimer()
    {

    }
}

Upvotes: 3

Marcin
Marcin

Reputation: 3262

Simply add timer on the end of your method:

if (!string.IsNullOrWhiteSpace(value))
{
  System.Timers.Timer timer = new System.Timers.Timer(3000) { Enabled = true };
  timer.Elapsed += (sender, args) =>
    {
       this.InfoLabel(string.Empty);
       timer.Dispose();
    };
 }

Upvotes: 6

Devendra D. Chavan
Devendra D. Chavan

Reputation: 9011

You can use Timer to create an instance of a timer that waits for n seconds before firing the Elapsed event. In the elapsed event, you clear the label's Content.

As the timer is executed in a separate thread, the UI thread is not locked while the timer is counting i.e. you are free to perform other operations in the UI.

private delegate void NoArgDelegate();

private void StartTimer(int durationInSeconds)
{
    const int milliSecondsPerSecond = 1000;
    var timer = new Timer(durationInSeconds * milliSecondsPerSecond);
    timer.Start();
    timer.Elapsed += timer_Elapsed;
}

private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    var clearLabelTextDelegate = new NoArgDelegate(ClearLabelText);
    this.Dispatcher.BeginInvoke(clearLabelTextDelegate);
}

private void ClearLabelText()
{
    this.myLabel.Content = string.Empty;
}

As I do not the rest of your code, some suggestions would be to create a lock on the timer so as to prevent more than one UI event starting the timer. In addition, the delegate and the timer instance can be made as private members of the class.

Upvotes: 1

C.Evenhuis
C.Evenhuis

Reputation: 26446

You'll always be using at least the GUI thread. If you decide to wait on that thread, no other interaction with controls is possible (ie. no buttons will work, the window will not be repainted).

Alternatively you could use a System.Windows.Forms.Timer that gives control back to the OS, or another type of timer. Either way, the "countdown" will either block user interaction or happen on another thread (under the hood).

Upvotes: 0

Related Questions