我的博客

posts(42) comments(30) trackbacks(5)
  • 博客园
  • 联系
  • 订阅 订阅
  • 管理

News

昵称:Jackey
园龄:5年
粉丝:2
关注:0

搜索

 

常用链接

随笔分类

  • Ajax编程(3)
  • C# Winform(5)
  • Javascript(11)
  • Office操作(2)
  • SQL Server(6)
  • vs2005 C# .net(5)
  • 通用函数(11)

随笔档案

  • 2008年2月 (1)
  • 2007年11月 (1)
  • 2007年10月 (9)
  • 2007年9月 (6)
  • 2007年7月 (1)
  • 2007年6月 (1)
  • 2007年5月 (13)
  • 2007年4月 (4)
  • 2007年1月 (6)

文章分类

  • 成功学(1)
  • 基金由浅到深(8)
  • 我写的文章(2)
  • 心情日记(4)

相册

  • 我的相册

最新评论

阅读排行榜

评论排行榜

推荐排行榜

View Post

C#只允许启动一个WinFrom进程

方法一:只禁止多个进程运行
[STAThread]
public static void Main()
{

    bool ret;
    System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);
    if (ret)
    {  System.Windows.Forms.Application.EnableVisualStyles();   //这两行实现   XP   可视风格   
        System.Windows.Forms.Application.DoEvents();
        System.Windows.Forms.Application.Run(new Main());
        //   Main   为你程序的主窗体,如果是控制台程序不用这句   
        mutex.ReleaseMutex();
    }
    else
    {
        MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        //   提示信息,可以删除。   
        Application.Exit();//退出程序   
    }
}

方法二:禁止多个进程运行,并当重复运行时激活以前的进程
[STAThread]
public static void Main()
{
    //Get   the   running   instance.   
    Process instance = RunningInstance();
    if (instance == null)
    {  System.Windows.Forms.Application.EnableVisualStyles();   //这两行实现   XP   可视风格   
        System.Windows.Forms.Application.DoEvents();
        //There   isn't   another   instance,   show   our   form.   
        Application.Run(new Main());
    }    
    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 = 1;

绿色通道:好文要顶关注我收藏该文与我联系

posted on 2007-10-02 15:55 Jackey 阅读(1102) 评论(3) 编辑 收藏

View Comments

915362
#1楼  回复 引用 查看   
不错
2007-10-03 13:56 | jillzhang      
#2楼  回复 引用 查看   
方法二很不错,我喜欢!
2007-10-03 22:54 | 大豆男生      
#3楼  回复 引用   
是还不错的。
2007-10-06 14:15 | w[未注册用户]
注册用户登录后才能发表评论,请 登录 或 注册,返回博客园首页。
首页博问闪存新闻园子招聘知识库
最新IT新闻:
· 微软任命新全球公共业务部门副总裁
· 职业社交网LinkedIn拟在移动应用中植入广告
· 企业微博:信息流通以人为本
· 分析师称谷歌进军硬件领域没有好结果
· 三十秒带你看尽每一款苹果产品,每一款NeXT产品
» 更多新闻...
最新知识库文章:
· 如何学习一门新的编程语言?
· 学习不同编程语言的重要性
· 为什么我喜欢富于表达性的编程语言
· 计算机专业的女生为什么要学编程
· 前端必读:浏览器内部工作原理
» 更多知识库文章...

China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
 
Powered by:
博客园
Copyright © Jackey