我想实现如下功能:如果程序已经启动`提示并将程序设置为当前窗体(焦点),没有启动就启动程序,保持当前系统中只有1个程序在运行.
请各位给点思路和有注释的源码,最好能帮我分析一下`谢谢`

解决方案 »

  1.   


    using System;using System.Runtime.InteropServices;using System.Windows.Forms;using System.Diagnostics;using System.Reflection;public class OneInstnace
    {
        [STAThread]
        public static void Main()
        {
            Process instance = RunningInstance();
            if (instance == null)
            {
                Application.Run (new Form());
            }
            else
            {
                HandleRunningInstance(instance);
            }    }    public static Process RunningInstance()
        {
              Process current = Process.GetCurrentProcess();
              Process[] processes = Process.GetProcessesByName (current.ProcessName);
              foreach (Process process in processes)
              {
                  //主要实现思想就是遍历进程列表,如果当前进程在列表中已存在,则激活当前进程,如果不存在,则创建该进程。
                  if (process.Id != current.Id)
                  {
                    if (Assembly.GetExecutingAssembly().Location.Replace(
    "/", "\\") == current.MainModule.FileName)
                    {
                        return process;
                    }
              }
            }
            return null;
        }    public static void HandleRunningInstance(Process instance)
        {
            ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
            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;
    }
      

  2.   

    SELECT TOP 1 *
    FROM DiQu
    WHERE (ID < 54)  -> 改为当前记录的ID
    ORDER BY ID DESC
      

  3.   

    using System.Diagnostics;`是主要是干什么用的`还有`如果进程列表中没有怎么来找呢?
    我的问题不是没可能的`我见现在一个劲舞团外挂就是在进程中找不到的,各位能帮帮说说隐藏进程的原理以及方法吗?
      

  4.   

    在Load事件是写上:
    bool bExist;
    Mutex MyMutex=new Mutex(true,"OnlyRunOncetime",out bExist);
    if(bExist)
    {
    Application.Run(new Form1());
    MyMutex.ReleaseMutex();
    }
    else
    {
    MessageBox.Show("程序已经运行!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
    }
    详细情况自己查MSDN去,我不想打字了