您的位置:程序门 -> .net技术 -> c#



程序打开以后,如果用户再次运行程序,如何控制只打开一个程序,第二次运行的时候,显示第一个运行的界面


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


程序打开以后,如果用户再次运行程序,如何控制只打开一个程序,第二次运行的时候,显示第一个运行的界面
发表于:2007-03-09 09:41:49 楼主
程序打开以后,如果用户再次运行程序,如何控制只打开一个程序,第二次运行的时候,显示第一个运行的界面
发表于:2007-03-09 09:43:201楼 得分:0
createmutex
发表于:2007-03-09 09:44:172楼 得分:0
如果检测到已运行   则发送wm_show消息
发表于:2007-03-09 09:49:023楼 得分:0
在program.cs中加入下面的代码   就可以实现程序只打开一次:
static   void   main()
                {
                        process   instance   =   runninginstance();
                        if   (instance   ==   null)
                        {
                                application.run(new   initial());                                
                        }
                        else
                        {                                
                                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;
}
发表于:2007-03-09 09:50:464楼 得分:0
using       system;      
    using       system.runtime.interopservices;      
    using       system.windows.forms;      
    using       system.diagnostics;      
    using       system.reflection;      
       
        public       class       oneinstnace      
        {      
       
        [stathread]      
        public       static       void       main()      
        {      
        //get       the       running       instance.      
        process       instance       =       runninginstance();      
        if       (instance       ==       null)      
        {      
        //there       isn 't       another       instance,       show       our       form.      
        application.run       (new       form());      
        }      
        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;      
        }
发表于:2007-03-09 09:51:365楼 得分:0
晕   网速一慢   被插队了...
发表于:2007-03-09 09:53:546楼 得分:0
来晚了
已经解决了
前段时间刚碰到这个问题
不过我的是.net   compact   framework   1.1的
发表于:2007-03-09 09:58:447楼 得分:0
就是用process類來解決。
发表于:2007-03-09 09:59:418楼 得分:0
学习


快速检索

最新资讯
热门点击