我是Windows服务c#的新手。我有一个名为JobAdminLib
的类库,它有一个类调用ArchiveAutomationAdministrator
。该类有一个名为CountJobs()
的方法。我创建了一个Windows服务,它将在预定的时间间隔运行此特定方法。但它似乎对我不起作用。日志报告说它正在运行,但该方法应该执行的功能不起作用。
我附上了参考代码
public class ArchiveAutomationAdministrator
{
JobRepository repository = new JobRepository();
public IEnumerable<LiveJobs> GetCurrentlyRetentionJobs(Func<LiveJobs, bool>
criteria = null)
{
return from job in repository.GetCurrentlyRetentionJobs() select job;
}
public void countJobs()
{
var count = from job in repository.GetCurrentlyRetentionJobs() select job;
int[] JobCount = new int[count.Count()];
for (int i = 1; i <= JobCount.Length; i++)
{
string jobnumber = repository.GetCurrentlyRetentionJobs().First().JobNumber;
JobAdministrator admin = new JobAdministrator(repository);
admin.ArchiveJob(jobnumber);
}
}
}
以下是我的Windows服务
public partial class Scheduler : ServiceBase
{
private Timer timer1 = null;
public Scheduler()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1 = new Timer();
this.timer1.Interval = 5000;
this.timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Tick);
timer1.Enabled = true;
Library.WriteErrorLog("test windows service started");
}
public void timer1_Tick(object sender, ElapsedEventArgs e)
{
this.task();
Library.WriteErrorLog("Job running successfully");
}
protected override void OnStop()
{
timer1.Enabled = false;
Library.WriteErrorLog("Service Stopped");
}
public void task()
{
Library.WriteErrorLog("Inside task");
ArchiveAutomationAdministrator admin = new ArchiveAutomationAdministrator();
admin.countJobs();
}
}
答案 0 :(得分:0)
检查Windows服务器是否有权运行
如果确定的话
转到Windows服务列表
1-开放运行cmd
2-型服务.msc
3-右键单击您的服务名称
4-在登录选项卡中单击本地系统帐户并选中允许服务与桌面交互
答案 1 :(得分:0)
该countJobs方法有一种枚举列表的疯狂方法。很难说这是否有效,但尝试以下......
public void countJobs()
{
foreach (var job in repository.GetCurrentlyRetentionJobs())
{
Library.WriteErrorLog("Archiving job " + job.JobNumber);
string jobnumber = job.JobNumber;
JobAdministrator admin = new JobAdministrator(repository);
admin.ArchiveJob(jobnumber);
}
}
通过这种方式,您将在循环中进行记录,并且您将能够判断是否有任何实际处理过程。