Download presentation
Presentation is loading. Please wait.
Published byDiane Rose Modified over 9 years ago
1
Lecture 16: Multithreaded Programming
3
public partial class Form1 : Form { Thread ct; Thread rt; public static int circle_sleep = 0; public static int rectangle_sleep = 0; public Form1() { InitializeComponent(); circle_sleep = trkCircles.Value; rectangle_sleep = trkRectangles.Value; } private void CircleThread() { int size = 0; System.Random rnd = new Random(); Graphics paper; paper = pictureBox1.CreateGraphics(); Pen pen = new Pen(Color.White); do { size = rnd.Next(50) + 5; pen.Color = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)); paper.DrawEllipse(pen, rnd.Next(295)+5, rnd.Next(295)+5, size, size); Thread.Sleep(circle_sleep); } while (true); }
4
private void btnCircles_Click(object sender, EventArgs e) { if (ct == null) { ct = new Thread(new ThreadStart(CircleThread)); ct.Start(); } else { try { ct.Abort(); } catch { } ct = null; } private void trkCircles_MouseUp(object sender, MouseEventArgs e) { circle_sleep = trkCircles.Value; } private void btnClear_Click(object sender, EventArgs e) { pictureBox1.Refresh(); } private void RectangleThread() { int size = 0; System.Random rnd = new Random(); Graphics paper; paper = pictureBox1.CreateGraphics(); Pen pen = new Pen(Color.White); do { size = rnd.Next(50) + 5; pen.Color = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)); paper.DrawRectangle(pen, rnd.Next(295) + 5, rnd.Next(295) + 5, size, size); Thread.Sleep(rectangle_sleep); } while (true); }
5
private void btnRectangles_Click(object sender, EventArgs e) { if (rt == null) { rt = new Thread(new ThreadStart(RectangleThread)); rt.Start(); } else { try { rt.Abort(); } catch { } rt = null; } private void trackBar1_MouseUp(object sender, MouseEventArgs e) { rectangle_sleep = trkRectangles.Value; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.