Answer to Question #10430 in C# for Sunshine

Question #10430
I have a program with multiple forms. From Form1 I want to open Form2 and simultanously display a graphic (some lines), but I get only a blank form. I can only get the graphic displayed if I click on a button, but I need it to appear without intervention. How can I do it?

Here is the code for the second form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WindowsApplication1
public partial class Form2 : Form
public Form2()


private void Form2_Load(object sender, EventArgs e)
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.SaddleBrown, 15);

g.DrawLine(p, 40, 400, 200, 400);
g.DrawLine(p, 80, 400, 80, 90);
g.DrawLine(p, 73, 90, 300, 90);
Expert's answer
Try drawing your graphics in a separate thread:
 Task.Factory.StartNew( () =>
                using (Graphics g = this.CreateGraphics())
Pen p = new Pen(Color.SaddleBrown,15);
                    g.DrawLine(p, 40, 400, 200, 400);
g.DrawLine(p, 73, 90, 300, 90);
                   g.DrawLine(p, 40, 400, 200, 400);
//do your drawing routines here
//invoke an action against the main thread to get the graphics on the form
this.Invoke( new Action(() =>


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be the first!

Leave a comment

New on Blog