进程类可以用来确定进程是否已经在运行吗?
我正在使用 进程类 用于启动进程,但不希望任何程序的多个实例运行。
查看文档,有很多看起来可能的属性,但没有什么是最明显的。
确定进程是否正在运行的最佳方法是什么?
编辑:John Fisher 是对的:这是我正在启动的现有应用程序,我无法对其进行修改。
I'm using the Process Class to start processes, but don't ever want more than one instance of any program to be running.
Looking at the documentation, there are lots of likely-looking properties, but nothing that stands out as the most obvious.
What's the best way to determine if a process is running?
Edit: John Fisher is right: it's an existing application that I'm starting and I'm unable to modify it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以调用此方法
并循环遍历结果(Process 类型的集合)以查看名称是否匹配。像这样的事情:
You can call this method
and loop through the result (a collection of type Process) to see if the name matches. Something like this:
我想这一切都取决于你所说的“最佳方式”是什么意思?你的意思是最快的、最准确的还是能处理一些奇怪情况的?
我开始的方法是列出进程并根据我要启动的进程检查可执行文件名。如果它们匹配(不区分大小写),则它可能正在运行。
I guess that all depends on what you mean by "best way"? Do you mean the fastest, the most accurate, or one that will handle some odd circumstances?
The way I would start is by listing the processes and checking the executable file name against the one I'm trying to start. If they match (case insensitive), it's probably running.
您应该为此使用单例应用程序模式:
You should use the Singleton application pattern for that: