欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#多線程Thread使用示例詳解

 更新時間:2021年07月11日 17:27:34   作者:空山新雨  
這篇文章主要為大家詳細介紹了C#多線程Thread使用示例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C#多線程Thread使用的示例代碼,供大家參考,具體內容如下

多線程:

線程生命周期狀態(tài)圖:

C#線程優(yōu)先級(概率高低):

基本使用示例:

using System;
using System.Threading;
 
namespace month_9_10._1009
{
    class Run5
    {
        /* 測試線程的調用過程
         * 主線程輸出world,子線程輸出hello
         */
        public static void showHello()
        {
            for(int i = 0; i < 100; i++)
            {
                Console.WriteLine($"Hello\t#{Thread.CurrentThread.Name}");
            }
        }
        public static void Main(string[] args)
        {
            Thread.CurrentThread.Name = "Main Thread!";
            var childThreadRef = new ThreadStart(showHello);
            Console.WriteLine("This is Main process!!!");
            var childThread = new Thread(childThreadRef);
            childThread.Name = "Child Thread!";
            childThread.Start();
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine($"World!\t#{Thread.CurrentThread.Name}");
            }
        }
    }
}

實例1:窗體標簽循環(huán)滾動

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
 
namespace RollMove
{
    public partial class Form1 : Form
    {
        Thread th1 = null;
        public Form1()
        {
            InitializeComponent();
            
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            int _sx = 40;
            int _ex = 280;
            int _top = 70;
            th1 = new Thread(() => {
                while (true)
                {
                    if (_sx <= _ex)
                    {
                        _ex = 280;
                        label1.Location = new Point(_sx, _top);
                        Thread.Sleep(20);
                        _sx += 5;
                    }
                    else
                    {
                        _ex = 40;
                        label1.Location = new Point(_sx, _top);
                        Thread.Sleep(20);
                        _sx -= 5;
                    }
                }
            });
            th1.Start();
        }
 
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (th1!=null)
            {
                th1.Abort();
            }
        }
    }
}

實例2:進度條

using System;
using System.Threading;
using System.Windows.Forms;
 
namespace month_9_10._1012
{
    public partial class Form_3 : Form
    {
        public static Print.Print print = Console.WriteLine;
        Thread th1, th2;
        public Form_3()
        {
            InitializeComponent();
            Text = "Welcome!";
            CheckForIllegalCrossThreadCalls = false;
            th1 = new Thread(new ThreadStart(Pro1));
            th1.Priority = ThreadPriority.Lowest;
            th1.Start();
            th2 = new Thread(new ThreadStart(Pro2));
            th2.Priority = ThreadPriority.Highest;
            //th2.Start();
            print(th1.Priority);
            print(th2.Priority);
            
        }
 
        void Pro1()
        {
            print("XXXXXXX");
            for (int i = 0; i <= 100; i++)
            {
                
                progressBar1.Value = i;
                label1.Text = $"{progressBar1.Value}%";
                Thread.Sleep(200);
                if (i == 20)
                {
                    th2.Start();
                    th2.Join();
                }
            }
        }
        void Pro2()
        {
            print("YYYYYYYY");
            for (int i = 0; i <= 100; i++)
            {
                progressBar2.Value = i;
                label2.Text = $"{progressBar2.Value}%";
                Thread.Sleep(100);
            }
        }
 
        private void progressBar1_Click(object sender, EventArgs e)
        {
            print("This is Main threading!");
        }
 
        private void Form_3_Load(object sender, EventArgs e)
        {
 
        }
 
        private void Form_3_FormClosing_1(object sender, FormClosingEventArgs e)
        {
            if (th1.ThreadState == ThreadState.Running)
                th1.Abort();
            if (th2.ThreadState == ThreadState.Running)
                th2.Abort();
        }
    }
}

實例三:線程同步(售票模擬)

using System;
using System.Threading;
 
namespace month_9_10._1012
{
    class Run2
    {
        static Print.Print print = Console.WriteLine;
        int _num = 10;
        void Ticket()
        {
            while (true)
            {
                //上鎖
                //lock (this)
                //{
                //    if (_num > 0)
                //    {
                //        Thread.Sleep(100);
                //        print(Thread.CurrentThread.Name + "--票數:" + _num--);
                //    }
                //}
 
                //放置監(jiān)視器
                Monitor.Enter(this);
                if (_num > 0)
                {
                    Thread.Sleep(100);
                    print(Thread.CurrentThread.Name + "--票數:" + _num--);
                }
                Monitor.Exit(this);
            }
        }
        static void Main()
        {
            var a1 = new Run2();
            Thread t1 = new Thread(new ThreadStart(a1.Ticket));
            t1.Name = "線程一";
            Thread t2 = new Thread(new ThreadStart(a1.Ticket));
            t2.Name = "線程二";
            Thread t3 = new Thread(new ThreadStart(a1.Ticket));
            t3.Name = "線程三";
            Thread t4 = new Thread(new ThreadStart(a1.Ticket));
            t4.Name = "線程四";
            t1.Start();
            t2.Start();
            t3.Start();
            t4.Start();
        }
    }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。 

相關文章

  • C#把EXCEL數據轉換成DataTable

    C#把EXCEL數據轉換成DataTable

    這篇文章介紹了C#把EXCEL數據轉換成DataTable的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#中的委托數據類型簡介

    C#中的委托數據類型簡介

    委托是一個類型安全的對象,它指向程序中另一個以后會被調用的方法(或多個方法)。通過本文給大家介紹C#中的委托數據類型簡介,對c委托類型相關知識感興趣的朋友一起學習吧
    2016-03-03
  • 詳解Unity中Mask和RectMask2D組件的對比與測試

    詳解Unity中Mask和RectMask2D組件的對比與測試

    本篇文章給大家介紹Unity中Mask和RectMask2D組件的對比與測試,包括組件用法及RectMask2D的基本用法,通過Mask的原理分析實例代碼相結合給大家講解的非常詳細,需要的朋友參考下吧
    2021-06-06
  • C# WinForm程序完全退出的問題解決

    C# WinForm程序完全退出的問題解決

    C# WinForm程序完全退出的問題解決,需要的朋友可以參考一下
    2013-05-05
  • C#使用SqlSugarClient進行數據訪問并實現了統(tǒng)一的批量依賴注入(示例代碼)

    C#使用SqlSugarClient進行數據訪問并實現了統(tǒng)一的批量依賴注入(示例代碼)

    M為 BaseDto 請用C# 給出一個案例,支持不同表對應不同的業(yè)務邏輯層,然后不同倉儲實例,不同表的業(yè)務,都實現統(tǒng)一的批量依賴注入,下面通過示例給大家演示如何使用SqlSugarClient進行數據訪問,并實現了統(tǒng)一的批量依賴注入,感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • 詳解C#中通過委托來實現回調函數功能的方法

    詳解C#中通過委托來實現回調函數功能的方法

    這篇文章主要介紹了C#中通過委托來實現回調函數功能的方法,文中舉了一個典型的多線程回調程序實例,需要的朋友可以參考下
    2016-04-04
  • C#基礎概念二十五問 21-25

    C#基礎概念二十五問 21-25

    C#基礎概念二十五問 21-25...
    2007-04-04
  • 深入分析C# Task

    深入分析C# Task

    這篇文章主要介紹了C# Task的相關資料,文中講解非常細致,代碼幫助大家更好的理解和學習C# Task的相關知識,感興趣的朋友可以了解下
    2020-08-08
  • C# 編碼好習慣,獻給所有熱愛c#的同志

    C# 編碼好習慣,獻給所有熱愛c#的同志

    c#編寫者,需要培養(yǎng)的一些好習慣
    2009-02-02
  • C#操作xml文件之Linq To Xml詳解

    C#操作xml文件之Linq To Xml詳解

    Linq To Xml,不看其他,單純的看名字都能感覺到,其可以使用靈活且強大的Linq語法,同時也可以使用lambda以及Xpath定位。本文將利用C#實現操作Linq To Xml,感興趣的可以了解一下
    2022-11-11

最新評論