Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Multithreaded drawing

Multithreaded drawing

Scheduled Pinned Locked Moved C#
graphicsdesignhelpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Groulien
    wrote on last edited by
    #1

    The line in my form is flickering. The form is DoubleBuffered which made me kind of lost :wtf: . Help?

    using System;
    using System.Windows.Forms;
    using System.Threading;
    using System.Drawing.Design;

    namespace Threading
    {
    public partial class Form1 : Form
    {
    Pen pen = new Pen(Color.Black);

        //I have an event that aborts the thread when closing the form.
        Thread t;
        public Form1()
        {
            InitializeComponent();
    
            pen.Width = 3;
            pen.StartCap = System.Drawing.Drawing2D.LineCap.SquareAnchor;
            pen.EndCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
                           //I used a ParameterizedThreadStart for debugging but I grew none the wiser.
            t = new Thread(new ParameterizedThreadStart(Commando));
            t.Start(true);
        }
        public void Commando(object obj)
        {
            while (true)
            {
                Graphics x = this.CreateGraphics();
                x.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                x.DrawLine(pen, new Point(0, 0),
                    new Point(
                   MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y));
    
                this.Refresh();
            }
        }
    }
    

    }

    Any help is appreciated.

    T N W 3 Replies Last reply
    0
    • G Groulien

      The line in my form is flickering. The form is DoubleBuffered which made me kind of lost :wtf: . Help?

      using System;
      using System.Windows.Forms;
      using System.Threading;
      using System.Drawing.Design;

      namespace Threading
      {
      public partial class Form1 : Form
      {
      Pen pen = new Pen(Color.Black);

          //I have an event that aborts the thread when closing the form.
          Thread t;
          public Form1()
          {
              InitializeComponent();
      
              pen.Width = 3;
              pen.StartCap = System.Drawing.Drawing2D.LineCap.SquareAnchor;
              pen.EndCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
                             //I used a ParameterizedThreadStart for debugging but I grew none the wiser.
              t = new Thread(new ParameterizedThreadStart(Commando));
              t.Start(true);
          }
          public void Commando(object obj)
          {
              while (true)
              {
                  Graphics x = this.CreateGraphics();
                  x.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                  x.DrawLine(pen, new Point(0, 0),
                      new Point(
                     MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y));
      
                  this.Refresh();
              }
          }
      }
      

      }

      Any help is appreciated.

      T Offline
      T Offline
      Tarakeshwar Reddy
      wrote on last edited by
      #2

      nbgangsta wrote:

      this.Refresh();

      This will not work, you are trying to refresh the form from a different thread. It will throw a cross thread reference error.

      nbgangsta wrote:

      t = new Thread(new ParameterizedThreadStart(Commando)); t.Start(true);

      Not sure what you are trying to do. But I would override the form's OnPaintMethod and use the MouseMove event to draw the line rather than using a different thread to draw it.


      Tarakeshwar Reddy There are two kinds of people, those who do the work and those who take the credit. Try to be in the first group; there is less competition there. - Indira Gandhi

      1 Reply Last reply
      0
      • G Groulien

        The line in my form is flickering. The form is DoubleBuffered which made me kind of lost :wtf: . Help?

        using System;
        using System.Windows.Forms;
        using System.Threading;
        using System.Drawing.Design;

        namespace Threading
        {
        public partial class Form1 : Form
        {
        Pen pen = new Pen(Color.Black);

            //I have an event that aborts the thread when closing the form.
            Thread t;
            public Form1()
            {
                InitializeComponent();
        
                pen.Width = 3;
                pen.StartCap = System.Drawing.Drawing2D.LineCap.SquareAnchor;
                pen.EndCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
                               //I used a ParameterizedThreadStart for debugging but I grew none the wiser.
                t = new Thread(new ParameterizedThreadStart(Commando));
                t.Start(true);
            }
            public void Commando(object obj)
            {
                while (true)
                {
                    Graphics x = this.CreateGraphics();
                    x.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    x.DrawLine(pen, new Point(0, 0),
                        new Point(
                       MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y));
        
                    this.Refresh();
                }
            }
        }
        

        }

        Any help is appreciated.

        N Offline
        N Offline
        ntrceptr
        wrote on last edited by
        #3

        Flickering is a sign that it is not doublebuffered or you are drawing on the wrong surface or flipping too soon. Double buffering is: Create a primary surface with one backbuffer (essentially 2 display surfaces) Always draw on the backbuffer (it is not visible) and when all drawing is complete swap the 2 surfaces. the swapping makes the backbuffer the primary and the primary becomes the backbuffer this is what prevents flickering, the instantanious swap of the source display surface. I haven't used the above drawing methods only DirectX and some GDI so i could be totally wrong about your code. It appears you are creating/initializing your drawing surfaces (this.CreateGraphics()) repeatedly....this should only be done once. - Where does it create your drawing surface Doublebuffered? - How does x.DrawLine know which surface of X to draw on? I also see "new" several times within you while(true) loop but no "delete" unless their is some sort of garbage collection for memory eventually you'll run out. Another issue you could have is since the drawing is in it's own thread....when the main form/window needs to refresh, it needs to know what to draw (the primary surface) or let the drawing thread know it needs to refresh the display area that it is handling. Hope this helped.

        1 Reply Last reply
        0
        • G Groulien

          The line in my form is flickering. The form is DoubleBuffered which made me kind of lost :wtf: . Help?

          using System;
          using System.Windows.Forms;
          using System.Threading;
          using System.Drawing.Design;

          namespace Threading
          {
          public partial class Form1 : Form
          {
          Pen pen = new Pen(Color.Black);

              //I have an event that aborts the thread when closing the form.
              Thread t;
              public Form1()
              {
                  InitializeComponent();
          
                  pen.Width = 3;
                  pen.StartCap = System.Drawing.Drawing2D.LineCap.SquareAnchor;
                  pen.EndCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
                                 //I used a ParameterizedThreadStart for debugging but I grew none the wiser.
                  t = new Thread(new ParameterizedThreadStart(Commando));
                  t.Start(true);
              }
              public void Commando(object obj)
              {
                  while (true)
                  {
                      Graphics x = this.CreateGraphics();
                      x.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                      x.DrawLine(pen, new Point(0, 0),
                          new Point(
                         MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y));
          
                      this.Refresh();
                  }
              }
          }
          

          }

          Any help is appreciated.

          W Offline
          W Offline
          William Winner
          wrote on last edited by
          #4

          I can't say for sure what's causing it, but I was curious about something in your code:

          public void Commando(object obj)
          {
          while (true)
          {
          Graphics x = this.CreateGraphics();
          x.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
          x.DrawLine(pen, new Point(0, 0),
          new Point(
          MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y));

                this.Refresh();
           }
          

          }

          you set it up as a parameterized void, and you passed it true, but then you never actually check the object. obj does absolutely nothing in this example. I don't think you're going to be able to get away from the flicker the way you're doing it. Why, can't you just put the code into the MouseMove? It will flicker a tiny bit while you're moving, but when the mouse is stopped, no flicker.

          private void Form1_MouseMove(object sender, MouseEventArgs e)
          {
          this.Refresh();
          Graphics x = this.CreateGraphics();
          x.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
          x.DrawLine(pen, new Point(0, 0),
          new Point(
          MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y));
          }

          Oh, and I modified your code to be more thread friendly:

          public void Commando(object obj)
          {
          while (true)
          {
          Graphics x = this.CreateGraphics();
          x.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
          x.DrawLine(pen, new Point(0, 0),
          new Point(
          MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y));

              if (this.InvokeRequired)
              {
                  RefreshMe = RefreshForm;
                  this.Invoke(RefreshMe);
              }
          
          }
          

          }

          private delegate void RefreshFormDel();

          private RefreshFormDel RefreshMe;

          private void RefreshForm()
          {
          this.Refresh();
          }

          private void Form1_FormClosing(object sender, FormClosingEventArgs e)
          {
          if (t != null)
          if (t.IsAlive)
          t.Abort();
          }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups