Run one instance from the application

前端 未结 7 959
盖世英雄少女心
盖世英雄少女心 2020-12-16 06:49

I have a windows application (C#) and i need to configure it to run one instance from the application at the time , It means that one user clicked the .exe file and the appl

7条回答
  •  情深已故
    2020-12-16 07:30

    I often solve this by checking for other processes with the same name. The advantage/disadvantage with this is that you (or the user) can "step aside" from the check by renaming the exe. If you do not want that you could probably use the Process-object that is returned.

      string procName = Process.GetCurrentProcess().ProcessName;
      if (Process.GetProcessesByName(procName).Length == 1)
      {
          ...code here...
      }
    

    It depends on your need, I think it's handy to bypass the check witout recompiling (it's a server process, which sometimes is run as a service).

提交回复
热议问题