问题描述
我是C#的新手.我有Windows服务.在此服务中,我运行一个线程.我想在线程完成后立即停止服务.目前,我的代码如下:
I'm a newbie in C#. I have a windows service. In this service, I run a thread. I want to stop the service immediately after the thread finish. Currently, my code is like:
while(true){
if(isThreadFinished){
MyService.Stop();
break;
}
}
它可以工作,但是我觉得这样使用while(true)是一种愚蠢的方法.因此,我想知道是否还有其他方法可以满足我的要求.
It work, but I feel that using while(true) like that is a stupid way. So, I want to know if there is any other way to achieve my request.
推荐答案
为什么不使用委托?您有一个示例,并且在此处,在MSDN官方网站上...
And Why don't use the delegate?You have a sample and the documentation here , in the official MSDN website...
您只需在线程结束之前调用您的委托方法即可.
You just call you're delegate method before the end of your Thread...
更合适的是while循环...如果线程长时间处于运行状态,则会更加优化...
That's more proprely that a while loop... and more optimize if your thread during a long time...
这篇关于线程完成后停止程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!