开始我是这样写的:
Dim lnPress() As Process = System.Diagnostics.Process.GetProcessesByName("bwDTS")
If lnPress.Length > 1 Then
    Me.Dispose()
End If结果发现启动前把程序的名子改一下这段代码就不起作用了,还能启动多次。
如何才能实现即使改了程序名也能判断出他是否已经启动过?

解决方案 »

  1.   

    Dim process() As Process
    process = Diagnostics.Process.GetProcessesByName(iagnostics.Process.GetCurrentProcess.ProcessName)
                    If UBound(process) > 0 Then
                        e.Cancel = True
                    End If
      

  2.   

    Dim process() As Process
    process = Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)
                    If UBound(process) > 0 Then
                        e.Cancel = True
                    End If
      

  3.   

    使用互斥量对象:
    互斥体有两种类型:局部互斥体和已命名的系统互斥体。如果使用接受名称的构造函数创建 Mutex 对象,则该对象与具有该名称的操作系统对象关联。已命名的系统互斥体在整个操作系统中都可见,可用于同步进程活动。您可以创建多个 Mutex 对象来表示同一个已命名的系统互斥体,也可以使用 OpenExisting 方法打开现有的已命名系统互斥体。局部互斥体仅存在于您的进程内。您的进程中任何引用局部 Mutex 对象的线程都可以使用它。每个 Mutex 对象都是一个单独的局部互斥体。// This example shows how a Mutex is used to synchronize access
    // to a protected resource. Unlike Monitor, Mutex can be used with
    // WaitHandle.WaitAll and WaitAny, and can be passed across
    // AppDomain boundaries.
     
    using System;
    using System.Threading;class Test
    {
        // Create a new Mutex. The creating thread does not own the
        // Mutex.
        private static Mutex mut = new Mutex();
        private const int numIterations = 1;
        private const int numThreads = 3;    static void Main()
        {
            // Create the threads that will use the protected resource.
            for(int i = 0; i < numThreads; i++)
            {
                Thread myThread = new Thread(new ThreadStart(MyThreadProc));
                myThread.Name = String.Format("Thread{0}", i + 1);
                myThread.Start();
            }        // The main thread exits, but the application continues to
            // run until all foreground threads have exited.
        }    private static void MyThreadProc()
        {
            for(int i = 0; i < numIterations; i++)
            {
                UseResource();
            }
        }    // This method represents a resource that must be synchronized
        // so that only one thread at a time can enter.
        private static void UseResource()
        {
            // Wait until it is safe to enter.
            mut.WaitOne();        Console.WriteLine("{0} has entered the protected area", 
                Thread.CurrentThread.Name);        // Place code to access non-reentrant resources here.        // Simulate some work.
            Thread.Sleep(500);        Console.WriteLine("{0} is leaving the protected area\r\n", 
                Thread.CurrentThread.Name);
             
            // Release the Mutex.
            mut.ReleaseMutex();
        }
    }