Single Instance

  • Print

If you wish that there can only be a single copy of your application built with AutoPlay Media Studio, TrueUpdate or Setup Factory running at all times, then you can use the sample Lua code below.

AutoPlay Media Studio

This sample will ask to close the application if you exit it manually, however if there is already another instance running, the new program will exit silently.

  • Place this into the Global Functions:
     
    function QueryAllowProjectClose()
      if (g_SilentShutdown) then
        return true;
      else
        -- ask for permission to close
        local result = Dialog.Message("Confirmation", "Are you sure that you want to quit?", 
          MB_YESNO, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
        if result == IDYES then
          -- end the application
          return true;
        else
          -- continue 
          return false;
        end
      end
    end
     
  • Place this into On Startup:
     
    local found = 0;
    -- set here the Window Title of your application, see Project > Settings
    local sWindowTitle = "Demo - Powered by AutoPlay Media Studio";
    tApplications = Window.EnumerateTitles(true);
    for handle, title in pairs(tApplications) do
      if (String.Find(title, sWindowTitle, 1, false) ~= -1) then
        found = found + 1;
      end
    end
    if (found > 0) then
      -- Another instance of the application is already running
      g_SilentShutdown = true;
      Application.Exit(0);
    else
      g_SilentShutdown = false;
    end
     

TrueUpdate

In this sample code, I assume that the executable is built using the default name, TrueUpdateClient.exe. If this is not the case, edit the script as required.

The same code can be used for Setup Factory or Visual Patch as well.

  • Place this at the beginning of the Client Script:
     
    local nFound = 0;
    local sExecutable = "trueupdateclient.exe"; -- process names are always lowercase
    local tProcesses = Window.EnumerateProcesses(true);
    local nMyWindowHandle = Application.GetWndHandle();
    local nProcessWindow = 0;
     
    for nWindowHandle, sProcessName in pairs(tProcesses) do
      sFile = String.SplitPath(sProcessName);
      -- strip the path and compare only filename and extension
      if (String.Lower(sFile.Filename .. sFile.Extension)) == sExecutable then
        nFound = nFound + 1;
        if (nMyWindowHandle ~= nWindowHandle) then
          nProcessWindow = nWindowHandle;
        end
      end
    end
     
    if nFound > 1 then
      -- try to bring the other application to the front
      Window.SetOrder(nProcessWindow,  HWND_TOP);
      -- as another sExecutable was found, do not proceed further
      Application.Exit(0);
    end
     
 

Add a comment (For questions, please use the contact form!)

Security code
Refresh

Our Plugins & Examples Feed

  • POP3 Actions plugin released

    A new, free actions plugin was released today. With it, the developer can check POP3...

  • Reminder: Update Serial Number Validation and/or Subscription Validation

    Next week IPInfoDB's API 2.0 will stop working. If you use an older version than...

Subscribe now!

Advertisement