您的位置:首页 > 编程语言 > C#

c# 线程操作实例

2016-08-31 08:51 375 查看
namespace 线程

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        /// <summary>

        /// 启动线程

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void Form1_Load(object sender, EventArgs e)

        {

            //Start()启动线程,告诉CPU可以执行我的任务了,具体何时执行,由CPU决定.

            //Abort()终止线程,终止完成后不能再Start()

            //Thread.Sleep(1),使当前线程停止一段时间运行.

            //Name 线程名

            //Thread.CurrentThread 获得当前的线程引用。

            this.label1.Text = "";

            this.label2.Text = "";

            Control.CheckForIllegalCrossThreadCalls = false;//取消程序交叉检查

        }

        private void button1_Click(object sender, EventArgs e)

        {

            text();

        }

        private void text()

        {

            for (int i = 0; i < 1000000; i++)

            {

                this.label1.Text = i.ToString();

            }

        }

        private void text2(object obj)

        {

            for (int i = 0; i < 1000000; i++)

            {

                this.label2.Text = obj.ToString();

            }

        }

        /// <summary>

        /// 线程调用无参函数

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button2_Click(object sender, EventArgs e)

        {

            Thread th = new Thread(text);

            th.IsBackground = true;

            th.Start();

        }

        /// <summary>

        /// 线程执行的函数若要带参,参数类型必须是object

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button3_Click(object sender, EventArgs e)

        {

            Thread th = new Thread(text2);

            th.IsBackground = true;

            th.Start("线程调用了带参数函数");

        }

        private void playgame()

        {

            Random r = new Random();

            while (true)

            {

                this.label1.Text = r.Next(0, 10).ToString();

                this.label2.Text = r.Next(0, 10).ToString();

            }

        }

      

        private void button4_Click(object sender, EventArgs e)

        {

            playgame();

        }

        private void button5_Click(object sender, EventArgs e)

        {

            Thread th = new Thread(playgame);

            th.IsBackground = true;

            th.Start();

        }

        bool b = false;

        private void playgame2()

        {

            Random r = new Random();

            while (b)

            {

                this.label1.Text = r.Next(0, 10).ToString();

                this.label2.Text = r.Next(0, 10).ToString();

            }

        }

        private void button6_Click(object sender, EventArgs e)

        {

            Random r = new Random();

            if (b == false)

            {

                b = true;

                button6.Text = "停止";

                Thread th = new Thread(playgame2);

                th.IsBackground = true;

                th.Start();

            }

            else

            {

                b = false;

                button6.Text = "新奖(2)";

            }

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: