4
Contributors
10
Replies
14
Views
8 Years
Discussion Span
Last Post by serkan sendur
Featured Replies
  • 1
    sknake 1,622   8 Years Ago

    Use a timer. [code=csharp] 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 = … Read More

1

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

  }
}
Votes + Comments
well written damn it, i am jealous
0

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?

0

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!

Edited by sknake: n/a

0

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

0

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

  }
}
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.