0
我在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多个线程抓取相同的作业并尝试打印。
让我知道你是否需要更多信息。
非常感谢您的意见。我对这项技术完全陌生,我想修改这段代码而不是编写新的代码。 – Lakhae