Hi,
I hope you guys can help me make this simple application work. I'm
trying to create a form displaying 3 circles, which independently
change colors 3 times after a random time period has passed.
I'm struggling with making the delegate/invoke thing work, as I know
GUI objects aren't thread-safe. I don't quite understand the concept
I'm supposed to use to modify the GUI thread-safe.
Below is my form and my Circle class. Currently, the application
crashes - I think because multiple threads are trying to modify the
Graphics object.
Thanks in advance for any help.
-Koschwitz
------------ start Form.cs -----------------------
public partial class Form1 : Form
{
private Random rnd;
private Pen myPen;
public Bitmap DrawArea;
Circle c1,c2,c3;
Graphics xGraph;
public Form1()
{
InitializeCompo nent();
rnd = new Random((int)Dat eTime.Now.Ticks ); // seeded with
ticks
myPen = new Pen(Color.Red);
DrawArea = new Bitmap(this.Cli entRectangle.Wi dth,
this.ClientRect angle.Height,
System.Drawing. Imaging.PixelFo rmat.Format24bp pRgb); //
make a persistent drawing area
xGraph = Graphics.FromIm age(DrawArea);
}
private void Form1_Load(obje ct sender, System.EventArg s e)
{
InitializeDrawA rea();
}
private void InitializeDrawA rea()
{
xGraph.Clear(Co lor.White);
}
private void Form1_Closed(ob ject sender, System.EventArg s e)
{
DrawArea.Dispos e();
}
public delegate void DrawCircleDeleg ate(); // no idea if this
is correct
private void Form1_Paint(obj ect sender,
System.Windows. Forms.PaintEven tArgs e)
{
xGraph = e.Graphics;
xGraph.DrawImag e(DrawArea, 0, 0, DrawArea.Width,
DrawArea.Height );
c1 = new Circle(xGraph, 450, 550, 900);
c2 = new Circle(xGraph, 450, 950, 500);
c3 = new Circle(xGraph, 450, 1350, 900);
Thread t1 = new Thread(new ThreadStart(c1. DrawCircle));
Thread t2 = new Thread(new ThreadStart(c2. DrawCircle));
Thread t3 = new Thread(new ThreadStart(c3. DrawCircle));
t1.Start();
t2.Start();
t3.Start();
xGraph.Dispose( );
}
}
------------ end Form.cs -----------------------
------------ start Circle.cs --------------------
class Circle
{
public Graphics xGraph;
private Random rnd = new Random((int)Dat eTime.Now.Ticks ); //
seeded with ticks
int r1, x1, y1;
public Circle(Graphics g, int r, int x, int y)
{
xGraph = g;
r1 = r;
x1 = x;
y1 = y;
}
public void DrawCircle()
{
SolidBrush Brush = new SolidBrush(Colo r.White);
for (int k = 1; k < 3; k++)
{
Brush.Color = Color.FromArgb(
(rnd.Next(0, 255)),
(rnd.Next(0, 255)),
(rnd.Next(0, 255)));
Control.Invoke( new DrawCircleDeleg ate(c1.DrawCirc le),
new object[] { }); //no idea if this is correct
xGraph.FillElli pse(Brush, x1 - r1, y1 - r1, r1, r1);
Thread.Sleep(rn d.Next(5000,100 00)); //random wait time
between 5 and 10 seconds
}
}
}
------------ end Circle.cs --------------------
I hope you guys can help me make this simple application work. I'm
trying to create a form displaying 3 circles, which independently
change colors 3 times after a random time period has passed.
I'm struggling with making the delegate/invoke thing work, as I know
GUI objects aren't thread-safe. I don't quite understand the concept
I'm supposed to use to modify the GUI thread-safe.
Below is my form and my Circle class. Currently, the application
crashes - I think because multiple threads are trying to modify the
Graphics object.
Thanks in advance for any help.
-Koschwitz
------------ start Form.cs -----------------------
public partial class Form1 : Form
{
private Random rnd;
private Pen myPen;
public Bitmap DrawArea;
Circle c1,c2,c3;
Graphics xGraph;
public Form1()
{
InitializeCompo nent();
rnd = new Random((int)Dat eTime.Now.Ticks ); // seeded with
ticks
myPen = new Pen(Color.Red);
DrawArea = new Bitmap(this.Cli entRectangle.Wi dth,
this.ClientRect angle.Height,
System.Drawing. Imaging.PixelFo rmat.Format24bp pRgb); //
make a persistent drawing area
xGraph = Graphics.FromIm age(DrawArea);
}
private void Form1_Load(obje ct sender, System.EventArg s e)
{
InitializeDrawA rea();
}
private void InitializeDrawA rea()
{
xGraph.Clear(Co lor.White);
}
private void Form1_Closed(ob ject sender, System.EventArg s e)
{
DrawArea.Dispos e();
}
public delegate void DrawCircleDeleg ate(); // no idea if this
is correct
private void Form1_Paint(obj ect sender,
System.Windows. Forms.PaintEven tArgs e)
{
xGraph = e.Graphics;
xGraph.DrawImag e(DrawArea, 0, 0, DrawArea.Width,
DrawArea.Height );
c1 = new Circle(xGraph, 450, 550, 900);
c2 = new Circle(xGraph, 450, 950, 500);
c3 = new Circle(xGraph, 450, 1350, 900);
Thread t1 = new Thread(new ThreadStart(c1. DrawCircle));
Thread t2 = new Thread(new ThreadStart(c2. DrawCircle));
Thread t3 = new Thread(new ThreadStart(c3. DrawCircle));
t1.Start();
t2.Start();
t3.Start();
xGraph.Dispose( );
}
}
------------ end Form.cs -----------------------
------------ start Circle.cs --------------------
class Circle
{
public Graphics xGraph;
private Random rnd = new Random((int)Dat eTime.Now.Ticks ); //
seeded with ticks
int r1, x1, y1;
public Circle(Graphics g, int r, int x, int y)
{
xGraph = g;
r1 = r;
x1 = x;
y1 = y;
}
public void DrawCircle()
{
SolidBrush Brush = new SolidBrush(Colo r.White);
for (int k = 1; k < 3; k++)
{
Brush.Color = Color.FromArgb(
(rnd.Next(0, 255)),
(rnd.Next(0, 255)),
(rnd.Next(0, 255)));
Control.Invoke( new DrawCircleDeleg ate(c1.DrawCirc le),
new object[] { }); //no idea if this is correct
xGraph.FillElli pse(Brush, x1 - r1, y1 - r1, r1, r1);
Thread.Sleep(rn d.Next(5000,100 00)); //random wait time
between 5 and 10 seconds
}
}
}
------------ end Circle.cs --------------------
Comment