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

推荐订阅源

N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
D
Docker
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
T
Tenable Blog
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Project Zero
Project Zero
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Scott Helme
Scott Helme
A
About on SuperTechFans
WordPress大学
WordPress大学
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
G
GRAHAM CLULEY
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

博客园 - 蔡秋心

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

        }