title: Animated gradient button flashes while painting
body: <p>I've created <code>GradientButton</code> which changes angle of gradient when mouse cursor is in its' bounds. Unfortunately the graphics is corrupted as there are random flashes during rendering.</p>
  
  <p>To achieve gradient rotation I start a thread upon <code>MouseEnter</code> and stop it at <code>MouseLeave</code>. Doublebuffered set to true, helped a lot but did not solve this fully.</p>
  
  <pre><code>  public partial class GradientButton : UserControl {
          public Color Color1 { get; set; }
          public Color Color2 { get; set; }
  
          public bool Down { get; set; }
          public bool MouseOnButton { get; set; }
  
          [Browsable(true)]
          public  string TextToDraw { get; set; }
          public int Angle { get; set; }
  
          public GradientButton() {
              InitializeComponent();
              Color1 = Color.YellowGreen;
              Color2 = Color.LightGreen;
          }
  
          protected override void OnPaint(PaintEventArgs e) {
              base.OnPaint(e);
              var color1 = Color1;
              var color2 = Color2;
              if (Down) {
                  var temp = color1;
                  color1 = color2;
                  color2 = temp;
              }
              if (MouseOnButton) {
                  color1 = ControlPaint.Dark(Color2);
              }
              using (LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, color1, color2, Angle)) {
                  e.Graphics.FillRectangle(brush, this.ClientRectangle);
              }
              Rectangle rect1 = ClientRectangle;
  
              // Create a StringFormat object with the each line of text, and the block
              // of text centered on the page.
              StringFormat stringFormat = new StringFormat();
              stringFormat.Alignment = StringAlignment.Center;
              stringFormat.LineAlignment = StringAlignment.Center;
  
              // Draw the text and the surrounding rectangle.
  
              e.Graphics.DrawString(TextToDraw, Font, new SolidBrush(ForeColor), rect1, stringFormat);       
          }
  
          protected override void OnClick(EventArgs e) {
              base.OnClick(e);
          }
          protected override void OnResize(EventArgs e) {
              base.OnResize(e);
              Invalidate();
          }
  
          protected override void OnMouseDown(MouseEventArgs e) {
              base.OnMouseDown(e);
              Down = true;
              Invalidate();
          }
  
          protected override void OnMouseUp(MouseEventArgs e) {
              base.OnMouseUp(e);
              Down = false;
              Invalidate();
          }
  
          protected override void OnMouseEnter(EventArgs e) {
              base.OnMouseEnter(e);
              MouseOnButton = true;
              Thread t = new Thread(Animate);
              t.Start();
  
          }
  
          public void Animate() {
              while (MouseOnButton) {
                  Angle += 5;
                  Thread.Sleep(25);
                  Invalidate();
              }
          }
          protected override void OnMouseLeave(EventArgs e) {
              base.OnMouseLeave(e);
              Angle = 0;
              MouseOnButton = false;
              Invalidate();
          }
      }
  </code></pre>
  
  <p>GradientButton.Designer.cs</p>
  
  <pre><code>partial class GradientButton {
      /// &lt;summary&gt; 
      /// Required designer variable.
      /// &lt;/summary&gt;
      private System.ComponentModel.IContainer components = null;
  
      /// &lt;summary&gt; 
      /// Clean up any resources being used.
      /// &lt;/summary&gt;
      /// &lt;param name="disposing"&gt;true if managed resources should be disposed; otherwise, false.&lt;/param&gt;
      protected override void Dispose(bool disposing) {
          if (disposing &amp;&amp; (components != null)) {
              components.Dispose();
          }
          base.Dispose(disposing);
      }
  
      #region Component Designer generated code
  
      /// &lt;summary&gt; 
      /// Required method for Designer support - do not modify 
      /// the contents of this method with the code editor.
      /// &lt;/summary&gt;
      private void InitializeComponent() {
          this.SuspendLayout();
          // 
          // GradientButton
          // 
          this.Name = "GradientButton";
          this.Size = new System.Drawing.Size(78, 28);
          this.ResumeLayout(false);
  
      }
  
      #endregion
  
  
  
  
  }
  </code></pre>