【发布时间】:2017-07-18 11:07:05
【问题描述】:
我正在开发一个 Windows 服务(在 Visual Studio 2008 中),当我在调试模式下运行它时它可以工作:
static void Main()
{
#if DEBUG
Descarga myServicio = new Descarga();
myServicio.OnDebug();
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#else
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Descarga()
};
ServiceBase.Run(ServicesToRun);
#endif
}
OnDebugStart() 只是调用 Windows 服务的OnStart() 方法,而运行的线程只是一个无限运行的线程(直到调用OnStop())来检查服务是否还活着并且在线。
public Timer myTimer;
public string fileName;
string txtLog;
public Descarga()
{
InitializeComponent();
myTimer = new Timer();
myTimer.Interval = 10000;
myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
}
protected override void OnStart(string[] args)
{
myTimer.Enabled = true;
}
当我在调试模式下启动它时,它工作得非常好,但是当我在发行版中运行它时,服务被托管,但代码不执行。
我更改了服务的用户,问题仍然存在。
亲切的问候
【问题讨论】:
-
你使用哪个
Timer类? -
@ToniWenzel - 从上下文来看,它似乎是
System.Timers.Timer。其他 FX Timer 类具有不同的属性/回调概念。 -
当您将其作为发布/服务运行时 - 您是否收到类似于“服务已启动但随后停止...”的消息?
标签: c# visual-studio visual-studio-2012