从线程池工作线程启动进程(如果需要则等待)
我有一个处理文件传输的应用程序。在某些情况下,我需要启动一些处理文件的前/后处理可执行文件。
因此,事件的顺序(简而言之)如下所示:
- 创建工作线程 工作线程
- 意识到在开始传输之前需要启动预处理可执行文件 预处理已启动,工作人员等待...如果等待太久,传输将不会发生,线程应正常完成
- 文件已传输
- 工作人员意识到传输完成后需要启动后处理可执行
- 文件后处理启动后,工作人员不关心等待
基本上,我不关心传输发生后后处理可执行文件运行多长时间。因此,如果我从一个线程启动该进程然后返回到池中,我是否应该预见到任何问题?
//后处理
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:
- Worker thread is created
- 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 - File is transferred
- Worker realizes it needs to launch a post-process executable after the transfer is finished
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这完全没有错。
想一想:
There is nothing wrong with that at all.
Think about it:
那是非常危险的。如果您在长时间运行的任务上耗尽了所有线程池线程,那么其他需要它们的事情将停止工作。您甚至可以使整个应用程序死锁。
规则很简单:
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: