Elephanting hell... You just won't listen.
Here. This creates a control called "LinesCanvas" you can drop on a Form. It demonstrates everything I've been telling you. There's 2 methods, you can call, Start and Stop.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace CsLines
{
public class LinesCanvas : Panel
{
Random RNG;
int lineCount = 100;
int stepLimit = 10;
Timer _timer;
List<Line> _lines;
Bitmap _canvas;
Pen _linePen;
public LinesCanvas()
{
this.DoubleBuffered = true;
SetupInternals();
}
public void Start()
{
SetupLines(lineCount);
_timer.Enabled = true;
}
public void Stop()
{
_timer.Enabled = false;
}
protected void SetupInternals()
{
RNG = new Random();
_linePen = new Pen(Color.LightBlue);
_timer = new Timer();
_timer.Enabled = false;
_timer.Interval = 10;
_timer.Tick += _timer_Tick;
}
protected override void OnResize(EventArgs eventargs)
{
base.OnResize(eventargs);
UpdateCanvasSize();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (_canvas != null)
e.Graphics.DrawImageUnscaled(_canvas, 0, 0);
}
private void _timer_Tick(object sender, EventArgs e)
{
UpdateLinePositions();
DrawLinesOnCanvas();
this.Invalidate();
}
protected void UpdateLinePositions()
{
foreach (Line line in _lines)
{
line.UpdatePosition(ClientSize);
}
}
protected void DrawLinesOnCanvas()
{
if (_canvas != null)
{
using (Graphics g = Graphics.FromImage(_canvas))
{
g.Clear(Color.Black);
foreach (Line line in _lines)
{
g.DrawLine(_linePen, line.EndPoint1, line.EndPoint2);
}
}
}
}
protected void UpdateCanvasSize()
{
if (_canvas != null)
_canvas.Dispose();
_canvas = new Bitmap(ClientSize.Width, ClientSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
}
protected void SetupLines(int count)
{
_lines = new List<Line>(lineCount);
for (int i = 0; i < count; ++i)
{
Line newLine = new Line(
RNG.Next(ClientSize.Width),
RNG.Next(ClientSize.Height),
RNG.Next(ClientSize.Width),
RNG.Next(ClientSize.Height),
RNG.Next(stepLimit) - 5,
RNG.Next(stepLimit) - 5,
RNG.Next(stepLimit) - 5,
RNG.Next(stepLimit) - 5
);
_lines.Add(newLine);
}
}
protected override void Dispose(bool disposing)
{
_linePen.Dispose();
base.Dispose(disposing);
}
}
}
Another class this needs, called "Line":
using System.Drawing;
namespace CsLines
{
public class Line
{
public Point EndPoint1;
public Point EndPoint2;
public Point Velocity1;
public Point Velocity2;
public Line(int x1, int y1, int x2, int y2, int vx1, int vy1, int vx2, int vy2)
{
EndPoint1 = new Point(x1, y1);
EndPoint2 = new Point(x2, y2);
Velocity1 = new Point(vx1, vy1);
Velocity2 = new Point(vx2, vy2);
}
public void UpdatePosition(Size clientSize)
{
EndPoint1.X += Velocity1.X;
EndPoint1.Y += Velocity1.Y;
EndPoint2.X += Velocity2.X;
EndPoint2.Y += Velocity2.Y;
if (EndPoint1.X < 0)
{
EndPoint1.X = 0;
Velocity1.X = -Velocity1.X;
}
if (EndPoint1.X > clientSize.Width)
{
EndPoint1.X = clientSize.Width;
Velocity1.X = -Velocity1.X;
}
if (EndPoint1.Y < 0)
{
EndPoint1.Y = 0;
Velocity1.Y = -Velocity1.Y;
}
if (EndPoint1.Y > clientSize.Height)
{
EndPoint1.Y = clientSize.Height;
Velocity1.Y = -Velocity1.Y;
}
if (EndPoint2.X < 0)
{
EndPoint2.X = 0;
Velocity2.X = -Velocity2.X;
}
if (EndPoint2.X > clientSize.Width)
{
EndPoint2.X = clientSize.Width;
Velocity2.X = -Velocity2.X;
}
if (EndPoint2.Y < 0)
{
EndPoint2.Y = 0;
Velocity2.Y = -Velocity2.Y;
}
if (EndPoint2.Y > clientSize.Height)
{
EndPoint2.Y = clientSize.Height;
Velocity2.Y = -Velocity2.Y;
}
}
}
}
Oh, and not a single goddamn thread anywhere.