设为首页
加入收藏
发布文章
  首页   操作系统   系统集成   程序设计   网站设计   图形图象   安全技术   反击技术   硬件DIY   认证考试   免费资源
当前位置:中国E盟 -=> 系统集成 -=> DNS服务器 -=> 正文  
保证应用程序只有一个实例在运行 << 已到尽头 | Visual C#建立简单消息传递系统(2) >> 
 发布日期:2006-6-18 16:13:14 发布者:[未知]  来源:[本站]  浏览:[]  评论:[ 字体:   
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);


//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
}


[VB.NET]
Public Shared Function RunningInstance() As Process
Dim current As Process = Process.GetCurrentProcess()
Dim processes As Process() = Process.GetProcessesByName(current.ProcessName)


'Loop through the running processes in with the same name
Dim process As Process
For Each process In processes
'Ignore the current process
If process.Id <> current.Id Then
'Make sure that the process is running from the exe file.
If [Assembly].GetExecutingAssembly().Location.Replace("/", "\") = current.MainModule.FileName Then
'Return the other process instance.
Return process
End If
End If
Next process
'No other instance was found, return null.
Return Nothing
End Function 'RunningInstance

如果检测到已有窗体实例,将此实例显示出来参考:

[STAThread]
static void Main()
{
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
{
//There isn't another instance, show our form.
Application.Run (new Form2());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
}
public static void HandleRunningInstance(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
//Set the real intance to foreground window
SetForegroundWindow (instance.MainWindowHandle);
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync( IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL
]]>
打印 】【 收藏 】【 推荐 】 
相关文章 推荐文章 热门文章
·暂无  ·暂无
字符串截取固定长度的方法(C#)
动态获取当前屏幕中光标所在位置的颜色
中英文语音合成与中文语音识别技术在c#..
linux下DNS服务器配置
C#.Net网络程序开发-Socket..
 评一评
正在读取…
  笔名:
  评论:
   
【注】 发表评论必需遵守以下条例:
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款

设置首页 - 加入收藏 - 关于我们 - 联系我们 - 友情连接 - -

Copyright ©2006-2008 中国E盟 Powered By:www.chinaE.org

湘ICP备05004075号