How do I find out if a process is already running using c#?

前端 未结 9 813
轻奢々
轻奢々 2020-12-07 23:15

I have C# winforms application that needs to start an external exe from time to time, but I do not wish to start another process if one is already running, but rather switch

9条回答
  •  隐瞒了意图╮
    2020-12-07 23:28

    In a past project I needed to prevent multiple execution of a process, so I added a some code in the init section of that process which creates a named mutex. This mutext was created and acquired before continuing the rest of the process. If the process can create the mutex and acquire it, then it is the first one running. If another process already controls the mutex, then the one which fails is not the first so it exits immediately.

    I was just trying to prevent a second instance from running, due to dependencies on specific hardware interfaces. Depending on what you need with that "switch to" line, you might need a more specific solution such as a process id or handle.

    Also, I had source code access to the process I was trying to start. If you can not modify the code, adding the mutex is obviously not an option.

提交回复
热议问题