How do I restrict my application to one instance?
http://www.c-sharpcorner.com/FAQ/Create1InstanceAppSC.asp
http://www.c-sharpcorner.com/FAQ/Create1InstanceAppSC.asp
解决方案 »
- vs2010对应的MSDN哪有下载?
- 如何遍历RichTextBox控件中的内容?
- 用dataset写入xml文件,出现的怪问题。请求帮助
- socket udp 事件
- 早上没事写了个文章分页方法
- gc自动回收好还是手动Gc.SuppressFinalize( this )好
- c#中streamwriter问题 .文本不能写入.帮忙看下了啊.谢了.
- 请帮忙看一段插入数据的代码!!不知错在哪里??谢谢!!
- 如何使用sqldmo实现sqlserver数据库异地备份
- 请教各位大哥,那里有关于用c#做web的资料,例如做留言版,论坛,登陆。。等等,请告知,本人不胜感激
- 一个问题,希望帮忙(winform)?
- 对文件的操作
string procId = "YourAPPName";
bool ownsmutex;
Mutex m;
m = new Mutex(true, procId, out ownsmutex);
if (!ownsmutex)
{
Console.WriteLine("程序已在运行!");
return;
}
//some process
using System.Diagnostics;
using System.Runtime.InteropServices;
class AppMain
{
[DllImport("user32.dll")] private static extern
bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")] private static extern
bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")] private static extern
bool IsIconic(IntPtr hWnd); private const int SW_HIDE = 0;
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10; static void Main()
{
// get the name of our process
string proc=Process.GetCurrentProcess().ProcessName;
// get the list of all processes by that name
Process[] processes=Process.GetProcessesByName(proc);
// if there is more than one process...
if (processes.Length > 1)
{
// Assume there is our process, which we will terminate,
// and the other process, which we want to bring to the
// foreground. This assumes there are only two processes
// in the processes array, and we need to find out which
// one is NOT us. // get our process
Process p=Process.GetCurrentProcess();
int n=0; // assume the other process is at index 0
// if this process id is OUR process ID...
if (processes[0].Id==p.Id)
{
// then the other process is at index 1
n=1;
}
// get the window handle
IntPtr hWnd=processes[n].MainWindowHandle;
// if iconic, we need to restore the window
if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd, SW_RESTORE);
}
// bring it to the foreground
SetForegroundWindow(hWnd);
// exit our process
return;
}
// ... continue with your application initialization here.
}
}