[英]How to solve this specific threading problem

Using the code from the following article, I implemented an own ThreadPool: http://www.developer.com/net/article.php/3783756


This is what I want to achieve: Triggered through a Timer, a service should query a database every 5 seconds for new jobs to execute. A Job is basically only the information about a commandline program that needs to be run with arguments.

這就是我想要實現的:通過Timer觸發,服務應該每5秒查詢一次數據庫,以便執行新的作業。 Job基本上只是有關需要使用參數運行的命令行程序的信息。

Up to 50 or more of these programs should be able to be executed at the same time. A program can be running a couple of seconds, minutes and also hours. The service needs to be in control of these programs at all times, i.e. it must be able to kill a program on request for instance.


Using the ThreadPool implementation from above, I startet to queue the programs to be executed and could see when the service indeed executed them. No problem so far. However, the mechanism here works like this:


The ThreadPool creates a workerthread and starts it. Whenever a program is queued, the workerthread notices this and calls a delegate that essentially instantiates a System.Diagnostics.Process object and starts the external program. The thread then is finished with its work and would be able to start further programs. However... when theres no program to start, an idle timer makes the threadmanager kill the thread and thus interrupt the Process that has been started.


This is not what I need. Does anyone here have an idea, how to handle the scenario I described better?


3 个解决方案


1) Why is the death of a thread in this process resulting in the death of the other, started process? If you answer that question, you'll have solved your problem.


2) That looks like a pretty lousy, and fairly naive, ThreadPool article. Check out Joe Duffy's series on a custom thread pool (part 1, part 2, and part 3). Code such as that is surprisingly intricate, and a significant maintenance burden on its own.

2)這看起來像一個非常糟糕,相當天真的ThreadPool文章。查看Joe Duffy關於自定義線程池的系列文章(第1部分,第2部分和第3部分)。諸如此類的代碼令人驚訝地錯綜復雜,並且本身就存在巨大的維護負擔。

3) (The real answer) Why are you using a threadpool to do this at all? Your threadpool only ever uses one thread at a time, why not just use a timer and trigger your main thread? Does your app do other stuff besides in a UI that you need to keep responsive?


Get rid of the threadpool, there's no need for it, it's making your life difficult, and threadpools in general are not designed to host long-running tasks. That's what individual threads are for. If you must trigger the timer on a separate thread, simply create a Thread to handle it and use this one, identical thread to spawn all your processes. You can then track your process state in one, central, sensible location. :)

擺脫線程池,沒有必要,它會讓你的生活變得困難,並且線程池通常不是為承載長時間運行的任務而設計的。這就是個人線程的用途。如果必須在單獨的線程上觸發計時器,只需創建一個線程來處理它,並使用這個相同的線程來生成所有進程。然后,您可以在一個中央,合理的位置跟蹤您的流程狀態。 :)


Are you sure that a thread pool is the optimal way of handling this? Spawning a new thread for each process which will be mostly idle but has to be present until the process terminates seems like a waste of a thread to me.


I would implement all this with a single thread and a dictionary of processes. The thread would periodically query the database and all the processes to see what actions need to be done.



AFAIK, processes spawned by Process.Start will continue to run even if the thread calling Process.Start exits. The following code illustrates this. LongRunningApp.exe will continue to run after the main program exits:


static void Main(string[] args)
    Process p = new Process();
    ProcessStartInfo psi = new ProcessStartInfo(@"C:\LongRunningApp.exe");
    psi.CreateNoWindow = true;
    psi.UseShellExecute = false;
    p.StartInfo = psi;



  © 2014-2022 ITdaan.com 联系我们: