代码之家  ›  专栏  ›  技术社区  ›  RBA

在Delphi应用程序中打开的互斥锁已签入C控制台应用程序

  •  1
  • RBA  · 技术社区  · 7 年前

    var
      AMutex: THandle;
    
    function OpenMutex(const AMutexName: String): Boolean;
    begin
    
      { Assume the Windows Mutext is already open }
      Result := False;
    
      { Is the Mutex already open? }
      if AMutex <> 0 then
        exit;
    
      { Try to create Windows Mutex }
      if CreateProgramMutex( AMutexName , AMutex) then
        Result := True
      else
        AMutex := 0;
    end;
    
     function CreateProgramMutex( AMutexName: string; var AMutex: THandle ): boolean;
    begin
        { Assume the new program mutex was created successfully. }
        Result := true;
    
        { Attempt to create a new mutex. }
        AMutex := CreateMutex(nil, False, PChar(AMutexName));
    
        { If we at least got a handle to the mutex... }
        if (AMutex <> 0) then
        begin
    
          if GetLastError = ERROR_ALREADY_EXISTS then begin
            { Close the handle, since it already exists. }
            CloseHandle(AMutex);
    
            { Set the return to show that it was already running. }
            Result := false;
          end;
        end else
          Result := false;
    end;
    

    我正在试着从C(作为初学者)了解我的应用程序是否已经在控制台应用程序中运行:

    using System;
    using System.Threading;
    
    namespace ConsoleApplication1
    {
        class OneAtATimePlease
        {
            private static Mutex _mutex;
    
            private static bool IsSingleInstance()
            {
                _mutex = new Mutex(false, "my mutex name");
    
                // keep the mutex reference alive until the normal 
                //termination of the program
                GC.KeepAlive(_mutex);
    
                try
                {
                    return _mutex.WaitOne(0, false);
                }
                catch (AbandonedMutexException)
                {
                    // if one thread acquires a Mutex object 
                    //that another thread has abandoned 
                    //by exiting without releasing it
    
                    _mutex.ReleaseMutex();
                    return _mutex.WaitOne(0, false);
                }
            }
            static void Main()
            {
                if (!IsSingleInstance())
                    Console.WriteLine("already running");
                Console.ReadLine();
    
            }
        }
    }
    

    PS:一切都是在同一个Windows用户会话下完成的

    2 回复  |  直到 7 年前
        1
  •  2
  •   Victoria zac    7 年前

    您说您的目标是检查一个外部应用程序是否正在运行(通过使用一个命名的互斥体)。对于这种情况,您不应该尝试在应用程序中创建具有给定名称的互斥对象,而应该只尝试打开这样的对象。原因很简单,如果外部应用程序使用这种互斥来检查它是否自己运行,那么实际上 这个互斥对象用于应用程序,而这个外部互斥对象永远不会启动。

    TryOpenExisting

    using System;
    using System.Threading;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Mutex mutex;
    
                if (Mutex.TryOpenExisting("My unique mutex name", out mutex)) {
                    try {
                        // with the used TryOpenExisting overload you can work with
                        // the mutex object here; you can wait for it or release
                        Console.WriteLine("Application is running!");
                    }
                    finally {
                        mutex.Close();
                    }
                }
                else {
                    Console.WriteLine("Application is NOT running!");
                }
                Console.ReadLine();
            }
        }
    }
    
        2
  •  2
  •   Ross Bush    7 年前

    Mutex appMutex = new Mutex(true, "MyMutex", out exclusive);
    if (!exclusive)
    {
      //Instance already existed
    }