如何在windows服务(c#)中调用我的方法?
我想创建一个每天早上4点自动运行的服务,该方法在调用时会自动删除一些文件,我已经编写了代码,但我在服务中调用的方法没有得到执行。下面我附上了我的代码,请有人给我一个解决方案如何在windows服务(c#)中调用我的方法?,c#,windows-services,C#,Windows Services,我想创建一个每天早上4点自动运行的服务,该方法在调用时会自动删除一些文件,我已经编写了代码,但我在服务中调用的方法没有得到执行。下面我附上了我的代码,请有人给我一个解决方案 public partial class Scheduler : ServiceBase { System.Timers.Timer _timer; public Scheduler() { InitializeComponent(); } protected over
public partial class Scheduler : ServiceBase
{
System.Timers.Timer _timer;
public Scheduler()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
_timer = new System.Timers.Timer();
_timer.Interval = 1 * 60 * 1000;//Every one minute
_timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
_timer.Start();
}
catch (Exception ex)
{
Library.WriteErrorLog(ex);
}
}
public void timer_Elapsed(object sender, ElapsedEventArgs e)
{
Library.WriteErrorLog("Timer elapsed");
this.ExtractDataFromSharePoint();
}
public void ExtractDataFromSharePoint()
{
Library.WriteErrorLog("inside task");
ArchiveAdministration admin = new ArchiveAdministration();
admin.ArchiveAuto();
Library.WriteErrorLog("job completed");
_timer.Stop();
}
}
并且我的日志文件只有以下输出语句:
计时器已过内部任务
我没有收到包含的“Job completed”语句。由于您没有收到Job completed行,因此在ExtractDataFromSharePoint方法中添加一个try-catch子句会有所帮助:
public void ExtractDataFromSharePoint()
{
try
{
Library.WriteErrorLog("inside task");
ArchiveAdministration admin = new ArchiveAdministration();
admin.ArchiveAuto();
Library.WriteErrorLog("job completed");
_timer.Stop();
}
catch(Exception ex)
{
Library.WriteErrorLog(ex.Message); //or try to log the stacktrace also
}
}
捕获异常,如果您无法解决它,请将其粘贴到此处以帮助您解决此问题我会在
ExtractDataFromSharePoint
中放置一个try…Catch块,以查看therehi@alaa_sayegh中发生了什么,通过在ExtractDataFromSharePoint()方法中添加try Catch,我将获得以下日志详细信息。谢谢,但我不知道如何纠正这个。。。2017年8月11日12:04:07:计时器已过2017年8月11日12:04:07:内部任务2017年8月11日12:04:17:无法打开登录请求的数据库“数据处理套件”。登录失败。用户“NT AUTHORITY\SYSTEM”登录失败。确定。授予用户NT AUTHORITY\SYSTEM访问数据库的权限。看到这个了吗:嗨,我现在有个问题(我的方法正在工作,但在任务完成后,服务不会在每一分钟后运行,如before@ShaktiS,调度程序与您所做的数据库设置无关。您需要检查您的方法是否在1分钟内完成,服务是否工作到结束并到达停止点。