我在 Windows 服务多线程项目的中间,我需要你们的一些输入才能成功运行它。下面是代码并描述了我正在尝试做的事情和问题。
// I created a new thread and call MyTimerMethod() from the Main method.
private void MyTimerMethod()
{
timer = Timers.Timer(5000)
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Start();
// make this thread run every time.
Application.Run();
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
for(int i = 0; i < SomeNum; i++) //SomeNum > 0
ThreadPool.QueueUserWorkItem(WaitCallback(MyWorkingMethod),null);
}
private void MyWorkingMethod(object state)
{
// each thread needs to go and check the status and print if currentStatus = true.
// if currentStautus = true then that jobs is ready to print.
// FYI ReadStatusFromDB() from the base class so I cannot modify it.
ReadStatusFromDB(); // ReadStatusFromDB() contains jobs to be printed.
// after doing some work store procedure update the currentStatus = false.
//do more stuff.
}
长话短说,程序每五秒运行一次,并检查是否还有更多工作要做。如果有则从线程池创建一个新线程并推入队列。现在我的问题是队列中有多个线程时。即使是 currentStatus = false 多个线程也会抓取相同的作业并尝试打印。
如果您需要更多信息,请告诉我。