惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
K
Kaspersky official blog
Forbes - Security
Forbes - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Proofpoint News Feed
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Scott Helme
Scott Helme
S
Securelist
美团技术团队
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
W
WeLiveSecurity
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
AI
AI
L
Lohrmann on Cybersecurity
S
Security Affairs
Cloudbric
Cloudbric
SecWiki News
SecWiki News
爱范儿
爱范儿
雷峰网
雷峰网
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
大猫的无限游戏
大猫的无限游戏
N
News and Events Feed by Topic
I
InfoQ
S
Secure Thoughts
AWS News Blog
AWS News Blog
A
About on SuperTechFans
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
The Last Watchdog
The Last Watchdog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Check Point Blog
P
Palo Alto Networks Blog
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google DeepMind News
Google DeepMind News
Latest news
Latest news
I
Intezer
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
D
Docker

博客园 - 蔡秋心

Windows Azure VM的两种shut down 方式 转贴: A Simple c# Wrapper for ffMpeg PowerShell: 找到一个目录下最新的文件 PowerShell: 如何解决File **.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get- help about_sig" for more de 在localhost上使用fiddler 在SQL Server Business Intelligence Development Studio中编辑Dynamcis CRM中的Report Report Design: Best Practices and Guidelines Visual Studio使用小技巧6 – 为代码加上Using(Resolve using)和管理Using(Organize using) Visual Studio使用小技巧5 – 区块选择(box selection)的拷贝(copy)和粘贴(paste) Visual Studio使用小技巧4 – Where am I(在Solution Explorer中显示当前文档) Visual Studio使用小技巧3 – 标签分组(Tab Group)和分割窗口(Split window) Visual Studio使用小技巧2 – 使用任务列表(task list) - 补充 Visual Studio使用小技巧2 – 使用任务列表(task list) Visual Studio使用小技巧1 – HTML编辑器中的格式化 部署Dotnetnuke Site到虚拟目录和端口不为80的网站 asp.net使用COM组件需要的权限设置 在Dynamics CRM 的 Entity Form中显示记录的ID的方法 如何使用VS2005创建web安装包 使用Visual Studio中的Item Template
前台线程(Foreground Threads)和后台线程(Background Threads)
蔡秋心 · 2010-07-13 · via 博客园 - 蔡秋心

不要将前台线程 (Foreground Threads) 和后台线程 (Background Threads)

指和常说的主线程 ( Primary Thread)和工作者线程 (Worker Thread) 混淆。

它们的定义如下 :

前台线程 (Foreground Threads):  前台线程可以阻止程序退出。除非所有前台线程都结束,否则 CLR不会关闭程序。

后台线程 (Background Threads) 有时候也叫 Daemon Thread 。他被 CLR 认为是不重要的执行路径,可以在任何时候舍弃。因此当所有的前台线程结束,即使还有后台线程在执行, CLR 也会关闭程序。

使用 Thread 类启动一个线程默认就是前台线程 (Foreground Threads) ,但是可以通过给 IsBackground 赋值将线程转变为后台线程。

例如 :

        public class Printer

        {

            public void PrintNumbers()

            {

                // Display Thread info.

                Console .WriteLine("-> {0} is executing PrintNumbers()" ,

                Thread .CurrentThread.Name);

                // Print out numbers.

                Console .Write("Your numbers: " );

                 for (int i = 0; i < 10; i++)

                {

                    Console .Write("{0}, " , i);

                    Thread .Sleep(2000);

                }

                Console .WriteLine();

            }

        }

        static void Main(string [] args)

        {

            Console .WriteLine("***** Background Threads *****\n" );

            Printer p = new Printer ();

            Thread bgroundThread =

                  new Thread (new ThreadStart (p.PrintNumbers));

            // This is now a background thread.

            bgroundThread.IsBackground = true ;

            bgroundThread.Start();

        }

另外从 ThreadPool 里面去的的线程默认是后台线程 (Background Threads)

例如 :

        static void Main(string [] args)

        {

            Console .WriteLine("***** Fun with the CLR Thread Pool *****\n" );

            Console .WriteLine("Main thread started. ThreadID = {0}" ,

            Thread .CurrentThread.ManagedThreadId);

            Printer p = new Printer();

            WaitCallback workItem = new WaitCallback (PrintTheNumbers);

            // Queue the method ten times.

            for (int i = 0; i < 10; i++)

            {

                ThreadPool .QueueUserWorkItem(workItem, p);

            }

            Console .WriteLine("All tasks queued" );

            Console .ReadLine();

        }

        static void PrintTheNumbers(object state)

        {

            Printer task = (Printer)state;

            task.PrintNumbers();

        }