你的位置:首页 > 软件开发 > ASP.net > C# 前台线程和后台线程

C# 前台线程和后台线程

发布时间:2016-07-26 18:00:17
进程会等待所有的前台线程完成后在结束工作,但是如果只剩下后台线程,则会直接结束工作 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.T ...

进程会等待所有的前台get='_blank'>线程完成后在结束工作,但是如果只剩下后台线程,则会直接结束工作

 

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);}}}}

原标题:C# 前台线程和后台线程

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录