﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>博客园-Todd.NET</title><link>http://www.cnblogs.com/weidagang2046/</link><description /><language>zh-cn</language><lastBuildDate>Mon, 08 Sep 2008 06:51:00 GMT</lastBuildDate><pubDate>Mon, 08 Sep 2008 06:51:00 GMT</pubDate><ttl>60</ttl><item><title>.NET后台线程</title><link>http://www.cnblogs.com/weidagang2046/archive/2008/07/08/1238343.html</link><dc:creator>weidagang2046</dc:creator><author>weidagang2046</author><pubDate>Tue, 08 Jul 2008 09:04:00 GMT</pubDate><guid>http://www.cnblogs.com/weidagang2046/archive/2008/07/08/1238343.html</guid><wfw:comment>http://www.cnblogs.com/weidagang2046/comments/1238343.html</wfw:comment><comments>http://www.cnblogs.com/weidagang2046/archive/2008/07/08/1238343.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/weidagang2046/comments/commentRss/1238343.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/weidagang2046/services/trackbacks/1238343.html</trackback:ping><description><![CDATA[.NET Thread分后台线程和前台线程，我们在创建线程的时候常常忽略了二者的区别，导致一些意外情况。下面举一个开发中遇到的实际问题来说明：<br />
<br />
现象：写一个GUI程序，然后通过new Thread启动一个新线程监听xxx端口；关闭GUI程序；通过windows资源管理器，发现进程还在。<br />
原因：new Thread创建线程默认是前台线程，只要有前台线程在，进程就不会结束。<br />
解决：把监听线程的IsBackground属性设为true。<br />
<br />
MSDN上有这样一段话：
<p>A
managed thread is either a background thread or a foreground thread.
Background threads are identical to foreground threads with one
exception: <span style="color: red;">a background thread will not keep the managed execution
environment alive.</span> Once all foreground threads have been stopped in a
managed process (where the .exe file is a managed assembly), the system
stops all background threads and shuts down. A thread can be designated
as a background or a foreground thread by setting the <a id="ctl00_rs1_mainContentContainer_ctl01" onclick="javascript:Track('ctl00_rs1_mainContentContainer_ctl00|ctl00_rs1_mainContentContainer_ctl01',this);" href="http://msdn.microsoft.com/en-us/library/system.threading.thread.isbackground%28VS.71%29.aspx">Thread.IsBackground</a>property. For example, a thread can be designated a background thread by setting <strong>Thread.IsBackground </strong>to <strong>true</strong>. A thread can likewise be designated a foreground thread by setting <strong>IsBackground </strong>to <strong>false</strong>.
All threads that enter the managed execution environment from unmanaged
code are marked as background threads. All threads generated by
creating and starting a new <strong>Thread </strong>object are foreground
threads. If you create a foreground thread that you want to listen for
some activity, such as a socket connection, you should set <strong>Thread.IsBackground </strong>to <strong>true</strong>, so that your process can terminate.</p>
<img src ="http://www.cnblogs.com/weidagang2046/aggbug/1238343.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/42144/" target="_blank">[新闻]千亿美元市值榜:微软居首移动第二 诺基亚退榜</a>]]></description></item><item><title>第一篇</title><link>http://www.cnblogs.com/weidagang2046/archive/2008/05/04/1182101.html</link><dc:creator>weidagang2046</dc:creator><author>weidagang2046</author><pubDate>Sun, 04 May 2008 09:44:00 GMT</pubDate><guid>http://www.cnblogs.com/weidagang2046/archive/2008/05/04/1182101.html</guid><wfw:comment>http://www.cnblogs.com/weidagang2046/comments/1182101.html</wfw:comment><comments>http://www.cnblogs.com/weidagang2046/archive/2008/05/04/1182101.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/weidagang2046/comments/commentRss/1182101.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/weidagang2046/services/trackbacks/1182101.html</trackback:ping><description><![CDATA[从事.NET开发一年有余，一直从.NET开发社区吸取营养，希望有机会可以贡献自己的一份力量！<br />
<img src ="http://www.cnblogs.com/weidagang2046/aggbug/1182101.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/42142/" target="_blank">[新闻]Google申请“海上数据中心”专利 可能付诸行动</a>]]></description></item></channel></rss>