不用API,而用.NET本身的类库可以吗?程序域什么的.

解决方案 »

  1.   

    一个方法是使用Mutex,因为Mutex是内核对象。static void Main() 
    {
        bool bRun=true;
        Mutex m=new Mutex(true,Application.ProductName,out bRun);

        if (bRun)
        {
    Application.Run(new WMainFrame());
    m.ReleaseMutex();
        }
        else
        {
    MessageBox.Show("已经有一个此程序的实例在运行","注意");
        }
    }
      

  2.   


    使用互斥体Mutex类型//using System.Threading;
        [STAThread]
        public static void Main(string[] args) 
        {
            Mutex mutex = new Mutex(false, "ThisShouldOnlyRunOnce");
            
             bool Running = !mutex.WaitOne(0, false);
             if (! Running)
                 Application.Run(new Form1());
             else
                 MessageBox.Show("已经过一次");
        }
      

  3.   

    1、利用Process.GetProcessesByName获得所有相同名字的Process,然后判断数组的
    Length属性是否大于1,大于1则代表有一个以上的进程。如:Module Main
      Sub Main()
        Dim [me] As Process = Process.GetCurrentProcess()
        Dim processes() As Process =
    Process.GetProcessesByName([me].ProcessName)
        If processes.Length > 1 Then
          Return
        End If    Application.EnableVisualStyles()
        Application.Run(New Form1)
      End Sub
    End Module2、利用Mutex创建一个跨进程的lock,如果在指定时间内不能获得lock的话就证明lock
    在另一进程中,所以退出。可以根据情况指定一个较短的等待相应时间。如:Module Main
      Sub Main()
        Dim mutex As New Mutex(False, "ProcessLock")
        Dim locked As Boolean = Not mutex.WaitOne(100, True)
        If locked Then
          Return
        End If    Application.EnableVisualStyles()
        Application.Run(New Form1)    mutex.ReleaseMutex()
      End Sub
    End Module
      

  4.   

    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;
     }
     }
     }
      

  5.   

    http://expert.csdn.net/Expert/topic/2491/2491081.xml?temp=.2268488