Windows services 多线程在多线程应用程序中访问同一代码 我在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. } //我创建了一个新线程,并从Main方法调用MyTimerMethod()。 私有void MyTimerMethod() { 定时器=定时器。定时器(5000) timer.appeased+=新的ElapsedEventHandler(OneLassedTime); timer.Start(); //每次运行此线程。 Application.Run(); } private void OneLassedTime(对象源,ElapsedEventArgs e) { 对于(int i=0;i0 QueueUserWorkItem(WaitCallback(MyWorkingMethod),null); } 私有void MyWorkingMethod(对象状态) { //如果currentStatus=true,每个线程都需要检查状态并打印。 //如果CurrentStatus=true,则该作业已准备好打印。 //仅供参考,从基类中读取StatusFromDB(),因此无法修改它。 ReadStatusFromDB();//ReadStatusFromDB()包含要打印的作业。 //执行某些工作存储过程后,更新currentStatus=false。 //多做事。 }

Windows services 多线程在多线程应用程序中访问同一代码 我在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. } //我创建了一个新线程,并从Main方法调用MyTimerMethod()。 私有void MyTimerMethod() { 定时器=定时器。定时器(5000) timer.appeased+=新的ElapsedEventHandler(OneLassedTime); timer.Start(); //每次运行此线程。 Application.Run(); } private void OneLassedTime(对象源,ElapsedEventArgs e) { 对于(int i=0;i0 QueueUserWorkItem(WaitCallback(MyWorkingMethod),null); } 私有void MyWorkingMethod(对象状态) { //如果currentStatus=true,每个线程都需要检查状态并打印。 //如果CurrentStatus=true,则该作业已准备好打印。 //仅供参考,从基类中读取StatusFromDB(),因此无法修改它。 ReadStatusFromDB();//ReadStatusFromDB()包含要打印的作业。 //执行某些工作存储过程后,更新currentStatus=false。 //多做事。 },windows-services,threadpool,Windows Services,Threadpool,长话短说,程序每五秒钟运行一次,并检查是否还有更多的工作要做。如果有,则从线程池中创建一个新线程并推入队列。现在我的问题是当队列中有多个线程时。即使currentStatus=false,多个线程也会抓取相同的作业并尝试打印 如果您需要更多信息,请告诉我。我建议创建一个工作项列表,并将您的程序构建为生产者/消费者应用程序。当提交作业时(通过计时器滴答声或其他方式),它只是添加到集合中 然后,您将有一个或多个持久性线程在集合上等待一个。将项添加到集合时,其中一个等待线程将获取并处理该项 这种结构有

长话短说,程序每五秒钟运行一次,并检查是否还有更多的工作要做。如果有,则从线程池中创建一个新线程并推入队列。现在我的问题是当队列中有多个线程时。即使currentStatus=false,多个线程也会抓取相同的作业并尝试打印


如果您需要更多信息,请告诉我。

我建议创建一个工作项列表,并将您的程序构建为生产者/消费者应用程序。当提交作业时(通过计时器滴答声或其他方式),它只是添加到集合中

然后,您将有一个或多个持久性线程在集合上等待一个。将项添加到集合时,其中一个等待线程将获取并处理该项


这种结构有几个优点。首先,它阻止多个线程处理同一项。其次,它限制了并发处理项目的线程数量。第三,线程在集合上执行非忙等待,这意味着它们没有消耗CPU资源。唯一的缺点是您有多个持久线程。但是,如果您大部分时间都在处理项目,那么持久化线程根本就不是问题。

非常感谢您的输入。我对这项技术完全陌生,我想修改这段代码,而不是编写新代码。