首页上一页 1 下一页尾页 1 条记录 1/1页
请老师解答下,快学快练15章第4题,为什么程序运行一段时间后就回乱跳怎么回事?
发表在C#图书答疑
2021-10-26 悬赏:4 学分
《C#编程入门指南》第15章 多线程编程技术
是否精华
是
否
版块置顶:
是
否
以下为代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; using System.Drawing.Drawing2D; namespace _1564 { public partial class Form1 : Form { public Form1() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; } private Color[] colos = new Color [20] {Color.AliceBlue,Color.Aqua,Color.Aquamarine, Color.Azure,Color.Beige,Color.Bisque,Color.Black,Color.BlanchedAlmond,Color.Blue, Color.BlueViolet,Color.Brown,Color.BurlyWood,Color.CadetBlue,Color.Chartreuse,Color.Chocolate, Color.Coral,Color.CornflowerBlue,Color.Red,Color.RosyBrown,Color.RoyalBlue}; private Thread td; private bool falge ; private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = this.CreateGraphics(); Random rd = new Random(); Font font = new Font("黑体",40,FontStyle.Bold); falge = true; td = new Thread(() => { while (falge) { int i = rd.Next(0, 19); int j = rd.Next(0,19); g.DrawString(" 明 • 日 • 科 • 技 ", font, new SolidBrush(colos[i]), 50, 80); Thread.Sleep(3000); this.BackColor = colos[j]; Thread.Sleep(3000); } }); td.Start(); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { if (td != null) { if (td.ThreadState == ThreadState.Running) { falge = false; td.Abort(); } } } } }
于2021-10-26 19:22:40编辑