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

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
Jina AI
Jina AI
Stack Overflow Blog
Stack Overflow Blog
量子位
博客园_首页
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Forbes - Security
Forbes - Security
IT之家
IT之家
N
News and Events Feed by Topic
S
Security Affairs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Webroot Blog
Webroot Blog
Recorded Future
Recorded Future
L
LangChain Blog
Y
Y Combinator Blog
AI
AI
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
L
Lohrmann on Cybersecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google Online Security Blog
Google Online Security Blog
V2EX - 技术
V2EX - 技术
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
PCI Perspectives
PCI Perspectives
I
Intezer
T
Tenable Blog
G
Google Developers Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Troy Hunt's Blog
L
LINUX DO - 最新话题
云风的 BLOG
云风的 BLOG
C
CXSECURITY Database RSS Feed - CXSecurity.com
有赞技术团队
有赞技术团队
O
OpenAI News
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
C
Check Point Blog
Last Week in AI
Last Week in AI
S
Schneier on Security
Simon Willison's Weblog
Simon Willison's Weblog
Blog — PlanetScale
Blog — PlanetScale

博客园 - 蔡秋心

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

        }