是否将“线程"设置为背景 [英] Set Thread as background or not

查看:96
本文介绍了是否将“线程"设置为背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关在此站点上获得的一些建议的解释.我正在做一个新闻通讯发送应用程序,并且我的邮件是在单独的线程中发送的,因此该过程不会降低整个网站的速度. 几个人建议我将线程IsBackground属性设置为true. 我这样做了,但对它的作用也很好奇,所以我在谷歌上搜索了一下. 事实证明,将IsBackground属性设置为true表示如果在该线程仍在运行时进程关闭,则可以." . 或者像微软所说的那样任何剩余的后台线程都会停止并且无法完成."

I need an explanation regarding some advice I got on this site. I'm doing a newsletter sending app, and I have my mail sent in a seperate thread so the process doesn't slow down the whole web site. A couple of people advised me to set the threads IsBackground property to true. I did this, but was also courious about what this does, so I googled a bit. As it turns out, setting the IsBackground property to true indicates that "it's okay if the process shuts down while this thread is still running.". Or as microsoft puts it "Any remaining background threads are stopped and do not complete."

我不知道我是否用错误的方式,但是,将IsBackground属性设置为false更好,以便产生的线程可以完成有关主线程的工作吗?

I don't know if I got this the wrong way but, wouldn't it be better to leave the IsBackground property to false, so that the spawned thread can complete its work regarding the main thread?

推荐答案

如果设置"IsBackground = true",则将线程标记为非必需线程-因此,即使站点关闭,您也希望完成后台工作,我认为您的直觉是正确的,请将其保留为"IsBackground = false".

If you set "IsBackground=true", you are marking the thread as nonessential -- so if you want your background job to complete even if the site goes down, I think your intuition is correct, leave it as "IsBackground=false".

这不是万无一失的;产生的线程仍然是主线程的子代,因此,根据站点关闭的方式,它仍然可以关闭整个过程.如果您真的想确保无论站点本身发生什么情况,您的工作都能顺利完成,则应考虑将其提取到自己的流程中,例如WCF服务.

This is not foolproof; the spawned thread is still a child of the main thread, so depending on how the site goes down, it can still bring down the entire process. If you really want to be sure that your job can run to completion no matter what happens to the site itself, you should think about extracting it into its own process, such as a WCF service.

这篇关于是否将“线程"设置为背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆