C#---线程2

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值