Draw on a form by a separate thread

I think you will need to draw to a Bitmap, then in the OnPaint Method, draw that bitmap to the window. I will demonstrate in a moment.

As Hans pointed out, in the OnPaint method you are setting

formGraphics = e.Graphics;

but at the end of the method e.Graphics is disposed, so you can't use it anymore, if your code got to


you would get an exception.

So basically you need to have a global

Bitmap buffer = new Bitmap(this.Width, this.Height)

in your asynced thread you would invoke your drawing to that Bitmap you can use

Graphics g=Graphics.FromBitmap(buffer);//

To get a graphics object, but remember you have to


it or wrap it in a

using (Graphics g=Graphics.FromBitmap(buffer))
//do something here


I am going to play with it for a few moments and see if I can get you a working sample

EDIT Here's your working sample. I started a new form and tossed a button on it. I changed the mainform backgroundimagelayout to none.

I think you need to be using .net 4.0 or better, if not using this, let me know I can change it to match your version... I think.

//you need this line to use the tasks
using System.Threading.Tasks;

namespace WindowsFormsApplication1
public partial class Form1 : Form
    public Form1()

    public void Draw()
        Bitmap buffer;
        buffer = new Bitmap(this.Width, this.Height);
        //start an async task
        Task.Factory.StartNew( () =>
                using (Graphics g =Graphics.FromImage(buffer))
                    g.DrawRectangle(Pens.Red, 0, 0, 200, 400);
                    //do your drawing routines here
            //invoke an action against the main thread to draw the buffer to the background image of the main form.
            this.Invoke( new Action(() =>
                    this.BackgroundImage = buffer;


    private void button1_Click(object sender, EventArgs e)
        //clicking this button starts the async draw method




  1. Wayne

    • 2015/9/3

    I think you will need to draw to a Bitmap, then in the OnPaint Method, draw that bitmap to the window. I will demonstrate in a moment.

  2. Cole

    • 2021/2/10

    protected override void OnPaint(PaintEventArgs e) { formGraphics = e.Graphics; DisplayThread = new Thread(new ThreadStart(Draw)); DisplayThread.Start(); } private void Draw() { if (this.InvokeRequired) { this.Invoke(new DrawDelegate(this.Draw)); } else { Engine.Draw(formGraphics); } }

  3. Manuel

    • 2017/1/8

    I need help regarding the C# windows application program. Please tell me how I can draw the graphics in a separate thread. Please help!!

  4. Mylo

    • 2016/4/5

    if (data != null)// draw check { draw1 = true; pictureBox1.BeginInvoke(new Action(() => { pictureBox1.Invalidate(); })); //Draw Images in separate thread Thread drawThread1 = new Thread(new ThreadStart(DrawImagesI)); drawThread1.start();}

  5. Brian

    • 2015/6/3

    I noticed a while ago that form applications are very slow to draw. When I minimize/ maximize or go over it with another window, 

  6. Forrest

    • 2016/1/28

    I noticed a while ago that form applications are very slow to draw. When I minimize/ maximize or go over it with another window, I can see the form components redrawn and/or I can erase the form for a second or so. I was told it is because the same thread draws and does everything else, the

  7. Davis

    • 2021/2/2

    splashSrc - represents the source rectangle for the background draw; attr - an ImageAttributes object used for transparency when drawing the 

  8. Hayden

    • 2021/2/20

    At the time the main form is loaded, the UpdateUI() method is invoked to update the status bar. Then the splash form is shown and immediately after that, DoSomeWork() method is executed in a separate thread. By using this technique, the main form is displayed without any delay and the splash form is telling the user that the data is loading.

  9. Casen

    • 2018/3/20

    It looks like WPF graphics code running in a separate thread may not work at using Eto.Drawing; using Eto.Forms; using System.Threading;

  10. Drake

    • 2018/9/8

    If program called DrawGraph directly instead of invoking it in a separate thread, then the user interface would never get time to process its messages so you wouldn’t be able to move, resize, or close the form.

  11. Sanna

    • 2019/7/13

    Subroutine PlotValue is a bit odd. It cannot directly access the program's main form because controls (including the form) can only be modified by the thread 

  12. Marcelo

    • 2015/12/22

    ¶ Rendering in a Separate Thread. This is a good start at drawing in a Windows Forms application, but it has a flaw: calls to the render method block the GUI thread. This isn't a problem if your renderer is very fast, but as it gets slow your GUI will become less responsive.

  13. Hayden

    • 2018/11/17

    For example, if you start a separate thread, it cannot directly change Because the thread's code is located in the same Form class that 

  14. Trevor

    • 2016/9/24

    If your model advancement takes a lot of time, consider placing it in its own thread as well. This way your model, rendering system, and GUI can all be on separate threads. ¶ Download Source Code. This code targets .NET Framework, but it will also compile using .NET Core. View on GitHub: Form1.cs. Download this project: starfield.zip

  15. Hendrix

    • 2020/9/25

    Your application code can cause the Java GUI to stop responding to user input. This new thread does the hard work drawing our random circles, 

  16. Clark

    • 2018/6/3

    Spell checking happens on a separate thread (probably on a background thread—a In fact, if you switch to a different task and then back to your form, 

  17. Rudy

    • 2021/4/13

    The helix may be single , as in an ordinary screw , or double , in which case two separate threads , beginning on opposite sides of the surface of the 

Comments are closed.

Recent Posts