C#---线程2

来源:互联网 发布:米哈伊洛维奇 知乎 编辑:程序博客网 时间:2024/06/05 16:44

C#---线程2

1、线程执行带参数的方法

新建一个C#WINFORM程序,在窗体上添加一个按钮。

Form1.cs代码为:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Threading; //线程命名空间namespace 线程2{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            Thread th = new Thread(Test);            th.IsBackground = true;            th.Start();                    }        private void Test(string s)//Test方法带参数        {            for (int i = 0; i < 10000;i++ )            {                Console.WriteLine(i);            }        }    }}

程序会报错:


注意:(1)如果线程的执行方法需要参数,那么要求这个参数必须是object类型。

将Form1.cs代码改为:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Threading; //线程命名空间namespace 线程2{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            Thread th = new Thread(Test);            th.IsBackground = true;            th.Start("123");//Test()方法传参数时传给Start()                    }        private void Test(object s)        {            string str = (string)s;            for (int i = 0; i <10000;i++ )            {                Console.WriteLine(i);            }        }    }}


2、实例---摇奖机

(1)新建一个C#WINFORM程序,在窗体上添加一个按钮和三个label控件。


(2)Form1.cs代码为:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Threading; //线程命名空间namespace 线程2{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        bool b = false; //启停标志        Thread th;        private void button1_Click(object sender, EventArgs e)        {            if (b==false)               {                b = true;                button1.Text = "停止";  //开始后button变为停止                th = new Thread(playGame);                th.IsBackground = true;                th.Start();            }            else            {                b = false;                button1.Text = "开始";            }                       }                private void playGame() //产生随机数        {            Random r = new Random();            while (b)            {                label1.Text = r.Next(0, 10).ToString(); //为三个label分别赋值0-10之间随机数                label2.Text = r.Next(0, 10).ToString();                label3.Text = r.Next(0, 10).ToString();            }        }        private void Form1_Load(object sender, EventArgs e)        {            Control.CheckForIllegalCrossThreadCalls = false;    //        }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)        {            if (th != null)            {                th.Abort(); //结束这个线程            }        }            }}

运行效果

           

        

0 0