Hi there. I would like to know how to make a label blink. Like I have these 2 labels. I would like the first label to blink when the form is loaded. Then when I press a button, the blinking label will stop and I will have the second label's visibility pop up.

Thanks for any and all the help!

Use a timer.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Timer = System.Timers.Timer;

namespace daniweb.timer
{
  public partial class frmBlink : Form
  {
    private Timer timer;
    private long syncPoint;

    public frmBlink()
    {
      InitializeComponent();
      timer = new Timer();
      timer.Interval = 250; //1 sec
      timer.AutoReset = true;
      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
      syncPoint = 0;
    }

    private void frmBlink_Load(object sender, EventArgs e)
    {
      label2.Visible = false;
      timer.Start();
    }

    delegate void Blink();
    private void BlinkLabel()
    {
      Blink del = (Blink)delegate { label1.Visible = !label1.Visible; };
      if (label1.InvokeRequired)
        label1.Invoke(del);
      else
        del();
    }

    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
      long sync = System.Threading.Interlocked.CompareExchange(ref syncPoint, 1, 0);
      if (sync == 0)
      {
        try
        {
          BlinkLabel();
        }
        finally
        {
          syncPoint = 0;
        }
      }
    }

    private void button1_Click(object sender, EventArgs e)
    {
      timer.Stop();
      while (System.Threading.Interlocked.Read(ref syncPoint) != 0)
        System.Threading.Thread.Sleep(10);
      label1.Visible = false;
      label2.Visible = true;
    }

  }
}
commented: well written damn it, i am jealous +7

oh damn. Thanks dude. worked like a charm

But if I wanted to increase or decrease the speed that it's blinking at, I just need to increase 250, right? And to make it slower, I would just have to decrease the value?

To increase the blink speed you would decrease 250. The 250 means -- "Every 250ms toggle the label's visibility." So if you toggle the visibility every 1000ms it will be a very slow blink. If you toggle it every 1ms it will be a very fast blink. You might not even be able to see it, you would have to test :)

Please mark this thread as solved if you have found an answer to your original question and good luck!

oh lol okay. My bads. Thanks :D

oh I have another question about this =]

I had decided to add in another or so. And I want to ask how would I make it so, llike, I have the blinking label stop with timer.Stop(), and then I have the next label appear. But I would like for, after a while, after the new label appears, to revert back to the blinking label.

For example, I have a label already saying ''Status'' and under it is the blinking label, ''Waiting...''. When I press the button, the code will go and the new label will appear saying like, ''done'', then I want like, 5-10 seconds to pass and the label, ''Waiting...'' will appear again :D

i couldnt open the project, is it 2008?

I opened it without prevail o.o

skanke answer pweeze =]

i couldnt open the project, is it 2008?

Yes

Be sure to copy/paste this code. I change syncPoint all over the place and if one of the values gets moved over wrong then it wont work:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Timer = System.Timers.Timer;

namespace daniweb.timer
{
  public partial class frmBlink : Form
  {
    private object __timerLock;
    private bool TimerRunning
    {
      get
      {
        lock (__timerLock)
        {
          return timer.Enabled;
        }
      }
    }

    private Timer timer;

    /// <summary>
    /// 0: The timer is available for the next elapsed event
    /// 1: A thread is busy with the timer's elapsed event
    /// 2: The timer is in a stopped state
    /// </summary>
    private long syncPoint;

    public frmBlink()
    {
      InitializeComponent();
      __timerLock = new object();
      timer = new Timer();
      timer.Interval = 250; //0.25 sec
      timer.AutoReset = true;
      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
      syncPoint = 2; //NOTE the value change here
    }

    private void frmBlink_Load(object sender, EventArgs e)
    {
      labelStatus1.Visible = false;
      labelStatus2.Visible = false;
      //timer.Start();
      StartTimer();
    }

    delegate void Blink();
    private void BlinkLabel()
    {
      Blink del = (Blink)delegate { labelBlink.Visible = !labelBlink.Visible; };
      if (labelBlink.InvokeRequired)
        labelBlink.Invoke(del);
      else
        del();
    }

    private void StartTimer()
    {
      if (this.TimerRunning)
        throw new InvalidOperationException("The timer is already running.");
      if (System.Threading.Interlocked.Read(ref syncPoint) != 2)
        throw new InvalidOperationException("The syncPoint value is not in the correct state for the timer to start");
      lock (__timerLock)
      {
        System.Threading.Interlocked.Exchange(ref syncPoint, 0);
        timer.Start();
      }
    }
    private void StopTimer()
    {
      if (!this.TimerRunning)
        throw new InvalidOperationException("The timer is not running.");
      lock (__timerLock)
      {
        timer.Stop();
      }

      while (System.Threading.Interlocked.CompareExchange(ref syncPoint, 2, 0) != 2)
        System.Threading.Thread.Sleep(10);

      labelBlink.Visible = false;
    }

    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
      long sync = System.Threading.Interlocked.CompareExchange(ref syncPoint, 1, 0);
      if (sync == 0)
      {
        try
        {
          BlinkLabel();
        }
        finally
        {
          System.Threading.Interlocked.Exchange(ref syncPoint, 0);
        }
      }
    }

    
    private void buttonFirstOperationComplete_Click(object sender, EventArgs e)
    {
      StopTimer();
      labelStatus1.Visible = true;
    }

    private void buttonSecondOperationBegins_Click(object sender, EventArgs e)
    {
      labelStatus2.Text = "Second operating starting...";
      labelStatus2.Visible = true;
      StartTimer();

    }

    private void buttonSecondOperationCompleted_Click(object sender, EventArgs e)
    {
      labelStatus2.Text = "Second operating complete";
      StopTimer();
    }

  }
}

Thanks for the coding its help me too with the help of timer, i can blink my label.

Please mark this as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.