如何解决这个特定的线程问题

发布于 2024-07-23 22:58:01 字数 664 浏览 5 评论 0原文

使用以下文章中的代码,我实现了自己的线程池: http://www.developer.com/net/article.php/3783756

这就是我想要实现的目标: 通过计时器触发,服务应每 5 秒查询一次数据库以执行新作业。 作业基本上只是需要使用参数运行的命令行程序的信息。

最多应能够同时执行 50 个或更多这些程序。 一个程序可以运行几秒钟、几分钟甚至几个小时。 该服务需要始终控制这些程序,即它必须能够根据请求终止程序。

使用上面的 ThreadPool 实现,我开始对要执行的程序进行排队,并可以查看服务何时确实执行它们。 到目前为止没有问题。 然而,这里的机制是这样工作的:

ThreadPool 创建一个工作线程并启动它。 每当程序排队时,工作线程都会注意到这一点并调用一个委托,该委托本质上实例化 System.Diagnostics.Process 对象并启动外部程序。 然后该线程完成其工作并能够启动进一步的程序。 然而......当没有程序启动时,空闲计时器会使线程管理器杀死线程,从而中断已启动的进程。

这不是我需要的。 这里有人知道如何更好地处理我描述的场景吗?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

唱一曲作罢 2024-07-30 22:58:01

1)为什么这个进程中一个线程的死亡会导致另一个已启动进程的死亡? 如果你回答了这个问题,你的问题就解决了。

2) 这看起来是一篇相当糟糕、相当幼稚的 ThreadPool 文章。 查看 Joe Duffy 的 关于自定义线程池的系列 (第 1 部分第 2 部分第 3 部分)。 诸如此类的代码非常复杂,并且其本身就是一个巨大的维护负担。

3)(真正的答案)为什么要使用线程池来执行此操作? 您的线程池一次只使用一个线程,为什么不直接使用计时器并触发您的主线程呢? 除了需要保持响应的 UI 之外,您的应用程序是否还执行其他操作?

摆脱线程池,没有必要,它会让你的生活变得困难,而且线程池通常不是为托管长时间运行的任务而设计的。 这就是单独线程的用途。 如果您必须在单独的线程上触发计时器,只需创建一个线程来处理它并使用这个相同的线程来生成所有进程。 然后,您可以在一个集中、合理的位置跟踪您的流程状态。 :)

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.

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

我很坚强 2024-07-30 22:58:01

您确定线程池是处理此问题的最佳方法吗? 为每个进程生成一个新线程,该线程大部分处于空闲状态,但必须一直存在,直到进程终止,对我来说似乎是浪费线程。

我将使用单个线程和进程字典来实现所有这些。 该线程将定期查询数据库和所有进程以查看需要执行哪些操作。

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.

魄砕の薆 2024-07-30 22:58:01

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();
}

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;
    p.Start();
    Console.ReadLine();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文