从线程池工作线程启动进程(如果需要则等待)

发布于 2024-09-09 08:03:40 字数 895 浏览 1 评论 0原文

我有一个处理文件传输的应用程序。在某些情况下,我需要启动一些处理文件的前/后处理可执行文件。

因此,事件的顺序(简而言之)如下所示:

  1. 创建工作线程 工作线程
  2. 意识到在开始传输之前需要启动预处理可执行文件 预处理已启动,工作人员等待...如果等待太久,传输将不会发生,线程应正常完成
  3. 文件已传输
  4. 工作人员意识到传输完成后需要启动后处理可执行
  5. 文件后处理启动后,工作人员不关心等待

基本上,我不关心传输发生后后处理可执行文件运行多长时间。因此,如果我从一个线程启动该进程然后返回到池中,我是否应该预见到任何问题?


//后处理

        Process process = null;
        ProcessStartInfo psi = new ProcessStartInfo(executable, args);
        psi.UseShellExecute = true;

        try
        {
            process = Process.Start(psi);
            //The pre-process simply calls Process.WaitForExit(timeout value)
            launched = true;
        }
        catch (InvalidOperationException) { }
        catch (ArgumentException) { }
        catch (System.ComponentModel.Win32Exception) { }
        catch (System.IO.FileNotFoundException) { }

I have an application that processes file transfers. In some instances, I need to launch some pre/post processing executables that do stuff with the files.

So the order of events (in brief) would be like this:

  1. Worker thread is created
  2. Worker realizes it needs to launch a pre-process executable before starting the transfer
    Pre-process is launched, worker waits... if it waits too long, the transfer will not occur and the thread should finish gracefully
  3. File is transferred
  4. Worker realizes it needs to launch a post-process executable after the transfer is finished
  5. Post-process is launched, worker doesn't care to wait

Basically, I don't care how long the post process executable runs after the transfer has occurred. Therefore, should I anticipate any problems if I launch the process from a thread that is then returned to the pool?


//Post process

        Process process = null;
        ProcessStartInfo psi = new ProcessStartInfo(executable, args);
        psi.UseShellExecute = true;

        try
        {
            process = Process.Start(psi);
            //The pre-process simply calls Process.WaitForExit(timeout value)
            launched = true;
        }
        catch (InvalidOperationException) { }
        catch (ArgumentException) { }
        catch (System.ComponentModel.Win32Exception) { }
        catch (System.IO.FileNotFoundException) { }

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

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

发布评论

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

评论(2

漆黑的白昼 2024-09-16 08:03:40

这完全没有错。

想一想:

  • 将线程返回到线程池实际上并不意味着任何事情 - 线程仍然存在。
  • 进程不以任何方式依赖于其父线程或进程 - 进程可以生成子进程然后退出。

There is nothing wrong with that at all.

Think about it:

  • Returning a thread to the threadpool doesn't actually mean anything - the thread is still there.
  • Processes are not in any way dependent on their parent threads or processes - it's possible for a process to spawn a child process and then exit.
零度℉ 2024-09-16 08:03:40

那是非常危险的。如果您在长时间运行的任务上耗尽了所有线程池线程,那么其他需要它们的事情将停止工作。您甚至可以使整个应用程序死锁。

规则很简单:

  • 短而快:线程池线程
  • 长而慢:手动创建的线程

That's very dangerous. If you use up all of your thread pool threads on long-running tasks then other things that need them will stop working. You can even dead-lock your whole application.

The rule is simple:

  • Short and fast: Thread Pool Thread
  • Long and slow: Manually created thread
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文