C#——線程

1、線程執行帶參數的方法

新建一個C#窗體應用程序,在窗體上添加一個按鈕。

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(); //結束這個線程
            }
        }
        
    }
}
 

                                  

 

                                          

 

 

 

 

 

 

 

--------------------- 
作者:陽光下的Smiles 
來源:CSDN 
原文:https://blog.csdn.net/liyuqian199695/article/details/60571559 
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章