title: C# Windows forms Blink label
body: <p>In a windows forms dialog I have a <code>Listview</code> that contains almost 2000 rows. Each row has a checkbox. To make it easy for the user I have two <code>Labels</code> above the Listview to show any active filter AND how many rows are selected (checked).</p>
  
  <p>To emphasize when a filter or selection is changed, I want to make the label blink. Therefore I extended the <code>Label</code> class to support blink automatically when the text is changed.
  But it is not working!</p>
  
  <p>When I use <code>Windows.Forms.Timer()</code> the label <strong>might</strong> blink once or twice, but not always, and often it ends up hidden.</p>
  
  <p>When I use <code>System.Timers.Timer()</code> I get a <code>crossthreadException</code> even though I use <code>InvokeRequired</code>.</p>
  
  <p>WHAT IS WRONG :(</p>
  
  <pre><code>class BlinkLabel : Label
  {
      private int _blinkFrequency = 621;
      private int _maxNumberOfBlinks = 15;
      private int _blinkCount = 20;
      private bool _isBlinking = false;
      //System.Timers.Timer _timer = new System.Timers.Timer();
      Timer _timer = new Timer();
  
      public BlinkLabel(){}
      public BlinkLabel(int frequency, int maxNrBlinks)
      {
          _blinkFrequency = frequency;
          _maxNumberOfBlinks = 15;
      }
      protected override void OnTextChanged(System.EventArgs e)
      {
          base.OnTextChanged(e);
          if (!_isBlinking)
          {
              base.ForeColor = System.Drawing.Color.Purple;
              StartBlink();
          }
      }
      public void StartBlink()
      {
          this._isBlinking = true;
          base.Visible = true;
          this._timer.Interval = this._blinkFrequency;
          this._timer.Enabled = true;
          //this._timer.AutoReset = true;
          //this._timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
          this._timer.Tick += new EventHandler(_timer_Tick);
          this._timer.Start();
      }
  
      void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
      {
          //SYSTEM.TIMERS.TIMER() TICK
          if (this.InvokeRequired)
          {
              this.Invoke(new MethodInvoker(() =&gt; { this._timer_Elapsed(sender, e); }));
          }
          else
          {
              base.Visible = !base.Visible;
              this._blinkCount++;
              Application.DoEvents();
              if (this._blinkCount &gt;= this._maxNumberOfBlinks)
              {
                  this._timer.Stop();
                  this._blinkCount = 0;
                  base.Visible = true;
                  this._isBlinking = false;
              }
          }
      }
  
      void _timer_Tick(object sender, EventArgs e)
      {
          //WINDOWS.FORMS.TIMER TICK
          this.Visible = !this.Visible;
          this._blinkCount++;
          Application.DoEvents();
          if (this._blinkCount &gt;= this._maxNumberOfBlinks)
          {
              this._timer.Stop();
              this._blinkCount = 0;
              //base.Visible = true;
              this.Visible = true;
              this._isBlinking = false;
          }
      }
  }
  </code></pre>