C#Windows服务 – 启动,然后自动停止

C#Windows服务 – 启动,然后自动停止,第1张

概述C#Windows服务启动,然后自动停止

我正在按照MSDN演练:创buildwindows服务中的说明创build此windows服务,并且在成功安装后,我转到Services.msc以启动windows服务,并在完成启动之前,我收到以下消息:

本地计算机上的EIwindowsService服务启动,然后停止。 有些服务如果没有被其他服务或程序使用,则会自动停止。

我知道windows服务启动正常,因为有一个日志文件的条目,说明服务启动。 我做了一些研究,然后发布在这里,并从一些服务停止自动的答案指出,问题可能是OnStart方法抛出一个错误,或OnStart不开始线程。 所以我修改了我的代码,这样OnStart中唯一的东西就是两个定时器和日志条目的开始,因此不需要exception处理。 我还添加了一个线程来“跳转”到另一个方法。

我再次尝试windows服务,我知道它“移动”到线程指向的新方法,因为我在那里有一个日志条目,由于我正在做的一些转换,抛出了一个FormatException错误。 我注意到转换和windows服务仍然刚刚开始启动,然后自动停止。

如何在C#中列出目录时过滤文件

作为另一个用户启动windows进程,而不直接知道密码

如何开发windows应用程序

Environment.Username为同一用户(套pipe)提供不同的结果:替代/转换?

什么会导致进程停止重新创build?

进一步的研究表明,我可能需要一个循环来保持处理的方法,所以我从C – windows服务服务的信息,并build立一个无限的while循环。 我还发现可能会有垃圾回收,并为MSDN定时器类的示例部分中build议的定时器build立一个KeepAlive语句。 还是一样的问题。

在这一点上,我觉得我已经用尽了所有的研究,所以在这里发表我的问题是适当的。 我所有的代码都在下面,我会注意到在执行任何更改之前,我卸载了windows服务,删除了安装项目,并从C#代码中删除了安装程序。 然后,我进行了更改,并重新开始了演练中的指示,从指示如何设置安装程序的位置开始。 我每次都这样做,因为我发现如果我进行了更改并且没有卸载windows服务,请删除安装项目并删除安装程序,那么我的更改将不会对当前安装的windows服务生效。

任何援助,你可以给予最赞赏。 我将在这里再等15分钟,然后明天我会检查这个第一件事。

SERVICE1.cs

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.linq; using System.ServiceProcess; using System.Text; using System.Timers; namespace EIwindowsService { public partial class Service1 : ServiceBase { Logs.ErrorLog logfile = new Logs.ErrorLog(); private System.Threading.Thread onStartThread; public Service1() { InitializeComponent(); } protected overrIDe voID OnStart(string[] args) { try { iTimer.Start(); iTimer.Elapsed += new ElapsedEventHandler(iTimer_Elapsed); pTimer.Start(); pTimer.Elapsed += new ElapsedEventHandler(pTimer_Elapsed); onStartThread = new System.Threading.Thread(TimerValue); onStartThread.Start(); logfile.SendTolog("EIwindows Service started on " + GetDate()); } catch (ArgumentOutOfRangeException ex) { logfile.SendTolog("ArgumentOutOfRangeException","EIwindowsService\Service1.cs","OnStart()",ex); } //end of ArgumentOutOfRangeException CATCH statement } protected overrIDe voID OnStop() { iTimer.Stop(); pTimer.Stop(); logfile.SendTolog("EIwindowsService\Service1.cs","OnStop()","EIwindows Service stopped on " + GetDate()); } private voID TimerValue() { try { /*commented out because it was throwing an exception error*/ //double iTimerValue = Convert.Todouble(iTimer.ToString()); //double pTimerValue = Convert.Todouble(pTimer.ToString()); while (1 > 0) { //if (iTimerValue % 1800000 == 0) //if the timer hits the 30min mark //{ // logfile.SendTolog("Current iTimer Value = " + iTimerValue.ToString()); //} //if (pTimerValue % 1800000 == 0) //if the timer hits the 30min mark //{ // logfile.SendTolog("Current pTimer Value = " + pTimerValue.ToString()); //} GC.KeepAlive(iTimer); GC.KeepAlive(pTimer); } //TimerValue(); } catch (OverflowException ex) { logfile.SendTolog("OverflowException","TimerValue()",ex); } //end of OverflowException CATCH statement catch (ArgumentException ex) { logfile.SendTolog("ArgumentException",ex); } //end of ArgumentException CATCH statement catch (FormatException ex) { logfile.SendTolog("FormatException",ex); } //end of FormatException CATCH statement } private string GetDate() { string current = "No Date Recorded"; try { current = DateTime.Now.ToString("F"); } catch (FormatException ex) { logfile.SendTolog("FormatException","GetDate()",ex); } //end of FormatException CATCH statement return current; } //end of method GetDate private voID iTimer_Elapsed(object source,ElapsedEventArgs e) { try { iTimer.Stop(); importI(); iTimer.Start(); } catch (ArgumentOutOfRangeException ex) { logfile.SendTolog("ArgumentOutOfRangeException","iTimer_Elapsed()",ex); } //end of ArgumentOutOfRangeException CATCH statement } //end of method iTimer_Elapsed private voID pTimer_Elapsed(object source,ElapsedEventArgs e) { try { pTimer.Stop(); importP(); pTimer.Start(); } catch (ArgumentOutOfRangeException ex) { logfile.SendTolog("ArgumentOutOfRangeException","pTimer_Elapsed()",ex); } //end of ArgumentOutOfRangeException CATCH statement } //end of method pTimer_Elapsed private voID importI() { //does some action but commented out because it never gets here and is not relavant to this question. } //end of method importI private voID importP() { //does some action but commented out because it never gets here and is not relavant to this question. } //end of method importP } }

SERVICE1.DESIGNER.CS (相关的东西)

private voID InitializeComponent() { this.components = new System.ComponentModel.Container(); this.pTimer = new System.Timers.Timer(10800000); //3hrs this.iTimer = new System.Timers.Timer(3600000); //1hr // // pTimer // this.pTimer.Enabled = true; // // iTimer // this.iTimer.Enabled = true; // // Service1 // this.Servicename = "EIwindowsService"; } #endregion private System.Timers.Timer pTimer; private System.Timers.Timer iTimer;

如何在SavefileDialog的默认filename中设置长string(> 260)?

我可以在现有stream程上执行代码吗?

检测windows机器是否运行了病毒扫描程序?

如何更改windows Service环境path

如何在不活动窗口中模拟代码中的键盘事件?

你不需要创建一个单独的线程或担心垃圾收集器。 该框架处理所有为你。 只要创建计时器,他们将被调用。 这是一个例子。

public partial class Service1 : ServiceBase { private Timer timer; public Service1() { InitializeComponent(); } protected overrIDe voID OnStart(string[] args) { timer = new Timer(1000); timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); timer.Start(); } voID timer_Elapsed(object sender,ElapsedEventArgs e) { using (StreamWriter writer = file.AppendText(@"C:UsersalfonsoDesktoplog.txt")) { writer.Writeline(string.Format("{0} : {1}",DateTime.Now,"Logging from the service")); } } protected overrIDe voID OnStop() { } }

其他可能有助于有人遇到这个职位和上述解决方案不起作用。 当我遇到这个问题时,我已经添加了这个到我的windows服务的配置:

<system.web> <compilation deBUG ="true" /> </system.web>

我添加了这个,这样我就可以在本地运行时将调试器附加到服务上,但是当我尝试将服务移动到另一台服务器时,它给出了指定的错误。 通过从配置中删除这个服务再次工作。

总结

以上是内存溢出为你收集整理的C#Windows服务 – 启动,然后自动停止全部内容,希望文章能够帮你解决C#Windows服务 – 启动,然后自动停止所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1294313.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存