程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 前台線程和後台線程,

C# 前台線程和後台線程,

編輯:C#入門知識

C# 前台線程和後台線程,


進程會等待所有的前台線程完成後在結束工作,但是如果只剩下後台線程,則會直接結束工作

 

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.Diagnostics; namespace testThread { class Program { static void Main(string[] args) { var sampleForegroud = new ThreadSample(10); var sampleBackgroud = new ThreadSample(20); var threadone = new Thread(sampleForegroud.CountNumbers); threadone.Name = "ForegroudThread"; var threadtwo = new Thread(sampleBackgroud.CountNumbers); threadtwo.Name = "BackgroudThread"; threadtwo.IsBackground = true; threadone.Start(); threadtwo.Start(); } } class ThreadSample { private readonly int _iterations; public ThreadSample(int iterations) { _iterations = iterations; } public void CountNumbers() { for (int i = 0; i < _iterations; i++) { Thread.Sleep(TimeSpan.FromSeconds(0.5)); Console.WriteLine("{0} Print {1}", Thread.CurrentThread.Name, i); } } } } View Code

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved