关于Mutex的问题 我用Mutex让系统只运行一个实例,如果发现已有一个Mutex对象(程序已在运行),怎么获得这个程序的进程ID呢?我想获得程序的进程ID后让程序显示在最前端。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 参考: HWND hMutex=FindWindow(wc.lpszClassName,NULL);//获取互斥实例的窗口句柄 if(hMutex) { if (IsIconic(hMutex))//如果最小化 ShowWindow(hMutex,SW_RESTORE);//回复正常 SetForegroundWindow(hMutex);//显示窗体到最顶端 return FALSE; } [DllImport("User32.dll", EntryPoint = "FindWindow")]引入FindWindow后怎么用Mutex去找已运行的实例句柄? 只要构造一个Mutex就行了, 判断第三个out参数。public Mutex( bool initiallyOwned, string name, out bool createdNew) 我的意思是想在createdNew为FALSE时找到运行程序的句柄。 其实已经告诉你方向,你再深入一下就知道了,写了个示例给你看看吧:static class Program { [DllImport("User32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Process process = RuningInstance(); if (process == null) { Application.Run(new Form1()); } else { MessageBox.Show("应用程序已经在运行中"); HandleRunningInstance(process); } } private const int SW_SHOWNOMAL = 1; private static void HandleRunningInstance(Process instance) { ShowWindowAsync(instance.MainWindowHandle, SW_SHOWNOMAL);//显示 SetForegroundWindow(instance.MainWindowHandle);//当到最前端 } private static Process RuningInstance() { Process currentProcess = Process.GetCurrentProcess(); Process[] Processes = Process.GetProcessesByName(currentProcess.ProcessName); foreach (Process process in Processes) { if (process.Id != currentProcess.Id) { if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == currentProcess.MainModule.FileName) { return process; } } } return null; } } 还有疑问:bool canCreate;Mutex myMutex = new Mutex(true, "MutexNameString", out canCreate);if(canCreate){ ...}else{ IntPtr hWnd = FindWindow(lpClassName, null); ShowWindowAsync(hWnd, 1); SetForegroundWindow(hWnd);}这里的lpClassName我是通过spy++查到的,有没有什么函数可以直接取得呢?能不能通过已经存在的名称为"MutexNameString"的Mutex对象取得hWnd呢? http://0123.blog.163.com/blog/static/478831200710215312818/给你个链接 请参考这个http://www.cnblogs.com/mediar/archive/2005/08/25/223021.html 这种方法还是有问题,就是把程序改名字后运行就会出现问题。还是Mutex + FindWindow + spy++有效。 "未能启用约束,一行或多行中包含违反非空,唯一或者外键约束的值 "报表中查询视图时出错 水晶报表密码问题 vs2008制作安装包的疑问 C#怎么给存储过程提供空参数(null) js我用window.opener.location.href=window.opener.location.href;怎么更新不了父页面. 为什么删不了行? 如何读取excel特定行? 请教类继承中含有参数的问题(急!) C#中如何使用代码,返回本地机器上“我的文档”的地址? 如何实现并发任务? asp.net 网站管理工具只能在VS2005时用么 C#,调用自定义函数求解
if(hMutex)
{
if (IsIconic(hMutex))//如果最小化
ShowWindow(hMutex,SW_RESTORE);//回复正常
SetForegroundWindow(hMutex);//显示窗体到最顶端
return FALSE;
}
引入FindWindow后怎么用Mutex去找已运行的实例句柄?
bool initiallyOwned,
string name,
out bool createdNew
)
{
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); [DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Process process = RuningInstance();
if (process == null)
{
Application.Run(new Form1());
}
else
{
MessageBox.Show("应用程序已经在运行中");
HandleRunningInstance(process);
}
} private const int SW_SHOWNOMAL = 1;
private static void HandleRunningInstance(Process instance)
{
ShowWindowAsync(instance.MainWindowHandle, SW_SHOWNOMAL);//显示
SetForegroundWindow(instance.MainWindowHandle);//当到最前端
}
private static Process RuningInstance()
{
Process currentProcess = Process.GetCurrentProcess();
Process[] Processes = Process.GetProcessesByName(currentProcess.ProcessName);
foreach (Process process in Processes)
{
if (process.Id != currentProcess.Id)
{
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == currentProcess.MainModule.FileName)
{
return process;
}
}
}
return null;
}
}
bool canCreate;
Mutex myMutex = new Mutex(true, "MutexNameString", out canCreate);
if(canCreate)
{
...
}
else
{
IntPtr hWnd = FindWindow(lpClassName, null);
ShowWindowAsync(hWnd, 1);
SetForegroundWindow(hWnd);
}
这里的lpClassName我是通过spy++查到的,有没有什么函数可以直接取得呢?
能不能通过已经存在的名称为"MutexNameString"的Mutex对象取得hWnd呢?
http://www.cnblogs.com/mediar/archive/2005/08/25/223021.html
还是Mutex + FindWindow + spy++有效。