在 C# 中启动外部应用程序时是否使用线程
我正在创建一个监视 ftp 日志的 win 服务,当文件上传后,我想启动一个外部应用程序(如 powershell 脚本)来处理该文件。 我的问题是,当我这样做时,我是否想将其转移到另一个线程中,或者我应该等到它完成后再继续。
这个进程已经在一个线程中(该服务将监视多个服务器),并且线程启动线程的想法让我担心。 这是值得担心的事情还是我帽子里的锡纸太多了。
I'm creating a win service that monitors ftp logs, when a file has been uploaded I want to start an external application, like a powershell script, to do stuff with the file. my Question is do i want to spin this off into another thread when i do it or should I just wait until it finishes before moving on.
This process is already going to be in a thread (the service is going to be monitoring multiple servers) and the idea of threads starting threads worries me. Is this something to be worried about or is this a case of too much tinfoil in my hat.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,以模块化方式编码,不用担心线程。 如果以后您发现您的应用程序将从多线程方法中受益,那么就解决它。 如果您正交地构建组件,那么螺纹部分会更自然地配合。
对我来说,在应用程序一开始就解决线程问题总是感觉像是过早的优化。 首先构建组件,然后再考虑如何对它们进行线程化。
[编辑] 我绝不建议您完全不要考虑线程。 每个组件都需要构建为可供多个线程使用的潜力 - 这在所有应用程序中都是一种防御性且明智的做法。 我的意思是不要太担心应用程序将如何处理线程以及如何首先设置应用程序的线程管理。
Well, code it in a modular fashion and don't worry about threads. If, down the road, you find that your application will benefit from a multi-threaded approach then address it then. If you have build your components orthogonally then the threading part will fit more naturally.
Addressing threading concerns at the very beginning of an application always feel like premature optimization to me. Build the components first and worry about how to thread them later.
[Edit] I am in no way advising you to not think about threading at all. Every component needs to be build with the potential for use by multiple threads - this is a defensive and intelligent practice in all applications. What I meant was don't worry so much about how the application will handle threads and how to set up the thread management of the application first.
我认为更重要的问题是你从产生另一个线程中得到什么? 如果您不需要并行执行代码,那么就不要这样做。 如果这样做,应该没有问题。 如果您关心子线程创建自己的线程,请将线程创建委托给
ThreadPool
。I think the more important question is what do you get out of spawning another thread? If you don't need to have the code execute in parallel, then don't do it. If you do, there should be no problem. If you are concerned with the child thread creating its own thread, then delegate the thread creation to the
ThreadPool
.主要问题:您需要知道该过程的结果吗? 如果你可以一劳永逸,那就这么做——这更容易。 如果你需要结果,那就等待吧。
另外,您是否考虑过使用 FileSystemWatcher? 它远程工作。
The primary question: do you need to know the outcome of that process? If you can fire and forget, then do that - it's easier. If you need the outcome, then wait for it.
Also, have you considered using the FileSystemWatcher? It works remotely.
尽管有些偏离主题,但由于您提到您将启动 powershell 脚本,我想指出通过 powershell“运行空间”在进程内运行脚本的选项。 这是一个最小示例:
添加对
c:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll
Although somewhat off-topic, since you mentioned that you'll be launching a powershell script, I wanted to point out the option to run the script in-process via a powershell "runspace". Here's a minimal example:
add a reference to
c:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll