从应用程序运行一个实例 [英] Run one instance from the application

查看:131
本文介绍了从应用程序运行一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows应用程序(C#),我需要将其配置为从当时的应用程序运行的一个实例,这意味着一个用户点击.exe文件,运行应用程序,用户没有关闭这是正在运行,并需要运行下一个实例,所以它应该出现的第一个实例,而不是打开新的应用程序的第一个实例。

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 application is run and the user didn't close the first instance of the application that is being run and need to run a next instance so it should appear the first instance and not opening new one.

任何一个可以帮助我那怎么办?

can any one help me how to do that?

在此先感谢

推荐答案

我常通过检查具有相同名称的其他过程解决这个问题。这样做的好处/坏处是,你(或用户)可以通过重命名的EXE从检查靠边站。如果你不想让你很可能返回的进程对象。

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).

这篇关于从应用程序运行一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆