如何解決這個特定的線程問題

[英]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

使用以下文章中的代碼,我實現了一個自己的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.

應該能夠同時執行多達50個或更多這些程序。程序可以運行幾秒鍾,幾分鍾甚至幾小時。該服務需要始終控制這些程序,即它必須能夠根據請求殺死程序。

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:

使用上面的ThreadPool實現,我開始對要執行的程序進行排隊,並且可以看到服務確實執行它們的時間。到目前為止沒問題。但是,這里的機制是這樣的:

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.

ThreadPool創建一個workerthread並啟動它。每當程序排隊時,workerthread會注意到這一點並調用一個委托,該委托基本上實例化System.Diagnostics.Process對象並啟動外部程序。然后該線程完成其工作,並將能夠啟動進一步的程序。但是......當沒有程序啟動時,空閑計時器會使線程管理器終止線程,從而中斷已經啟動的進程。

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

這不是我需要的。這里有沒有人有想法,如何處理我描述的場景更好?

3 个解决方案

#1


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.

1)為什么在這個過程中線程的死亡導致另一個線程的死亡,開始的過程?如果你回答這個問題,你就可以解決問題了。

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?

3)(真正的答案)為什么你使用線程池來完成這個?你的線程池一次只使用一個線程,為什么不只是使用一個計時器並觸發你的主線程?除了需要保持響應的UI之外,您的應用還能做其他事嗎?

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. :)

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

#2


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.

我將使用單個線程和進程字典來實現所有這些。該線程將定期查詢數據庫和所有進程,以查看需要執行的操作。

#3


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:

AFAIK,即使調用Process.Start的線程退出,Process.Start生成的進程也將繼續運行。以下代碼說明了這一點。主程序退出后,LongRunningApp.exe將繼續運行:

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;
    p.Start();
    Console.ReadLine();
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/06/09/147bef6998c92a45dbdf40aa27bea625.html



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