在Delphi应用程序中打开了Mutex,并在C#控制台应用程序中进行了检查

时间:2018-06-19 13:03:18

标签: c# .net delphi c#-4.0 delphi-10.1-berlin

我正在启动Delphi应用,并为此创建一个互斥锁:

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();

        }
    }
}

即使Delphi应用程序正在运行,IsSingleInstance仍返回true。使用相同的Delphi代码在Delphi控制台应用程序中检查互斥体是可行的。我敢肯定这很明显,但是我不知道自己在做什么错。

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

2 个答案:

答案 0 :(得分:2)

我认为您需要检查互斥是否存在或已创建。

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

答案 1 :(得分:2)

您说您的目的是检查外部应用程序是否正在运行(通过使用命名的互斥锁)。好吧,在这种情况下,您不应尝试在应用程序中创建给定名称的互斥对象,而应尝试打开此类对象。原因很简单,如果外部应用程序使用这种互斥锁来检查其是否正在运行,则实际上您会为应用程序该互斥锁,而外部互斥锁将永远不会启动。

为您的目的使用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();
        }
    }
}
相关问题