Mega Code Archive

 
Categories / Delphi / Examples
 

Switch between one or many instances

Title: Switch between one or many instances Question: Under Win16, you could check the value of hPrevInst at startup. Under Win32, on hPrevInst you can't rely upon; we can create a uniquely named mutex. But if you want to control instances of forms (not the app) at runtime or change the behaviour at starttime with the commandline, here's a straight solution: Answer: The default check of the function is to allow one instance. But you can control the start of many instances at the commandline prompt with: : yourApplication.exe /allowinstances The function itself: If the window is running and you don't want multiple instance at the command line then no further creation of another instance is possible; the function letAllowInstances() returns false else true. uses HWND.windows, FindCmdLineSwitch.SysUtils function letAllowInstances(const mainForm, cmdLine: PAnsiChar): boolean; var keyWindow: HWND; begin result:= false; keyWindow:= 0; keyWindow:= FindWindow(mainForm, NIL); //check if delphi is at design time and creation is allowed if FindWindow('TAppBuilder',NIL) 0 then keywindow:= 0; if (keyWindow 0) and not FindCmdLineSwitch(cmdLine, true) then //then set existing instance ShowWindow(keyWindow, SW_SHOWNORMAL) else result:= true; end; and the integration call of the func() into the project-file: begin if letAllowInstances('TKeyHandlingDialogForm', 'allowinstances') then begin Application.Initialize; Application.CreateForm(TKeyHandlingDialogForm, KeyHandlingDialogForm); Application.CreateForm(TPasswordInputDialog, PasswordInputDialog); Application.CreateForm(TGeneratingKeyDialog, GeneratingKeyDialog); Application.Run; end; end. FindWindow will return a valid handle if it finds the window class in the current active session (desktop). While using a mutex will prevent the user to start an application allready running in another session. Link to mutex solution: http://www.delphi3000.com/articles/article_870.asp?SK=mutex ------------------------------------------------------------- Update 13.11.2008 a big tip is to close your application and restart it: uses ShellAPI; procedure RestartApplication(sender: TObject); begin shellExecute(handle, 'open', PChar(application.ExeName), NIL, NIL, SW_SHOWNORMAL); Application.Terminate; end; Update 25.6.2009 function winExecAndWait(const sAppPath: string; wVisible: word): boolean var si: TStartupInfo; pi: TProcessInformation; proc: THandle; begin result:= false; fillChar(si, sizeOf(TStartupInfo),0); with si do begin cb:= sizeOf(TStartupInfo); dwFlags:= STARTF_USESHOWWINDOW OR START_FORCEONFEEDBACK; wShowWindow:= wVisible; end; if NOT CreateProcess(NIL, pChar(sAppPath), NIL, NIL, false, normal_Priority_class, NIL, NIL, si, pi) then begin raise Exception.CreateFmt('error during run'+'errcode %d', [getLastError]); end; proc:= pi.hProcess; closeHandle(pi.hThread); if waitForSingleObject(proc, infinite) wait_failed then result:= true; closeHandle(proc); end; Happy new year with a Primer for Beginners: http://www.softwareschule.ch/download/maxbox_starter.pdf New release of maXbox: maXbox is a scripter tool with an inbuilt delphi engine in one exe! It is designed for teaching, test and analyzing methods and algorithms and runs under Windows and Linux (CLX) to set Delphi in a box without installation. The tool is based on an educational program with a lot of examples and exercises (from biorhythm to how a byte code compiler works). Now with a two part tutorial for beginners in programming. http://sourceforge.net/projects/maxbox/ ----------------------------------------------------------------------------------------- New release 3.2 of maXbox3: # Now almost 1170 functions /procedures and about 130 objects /classes of VCL, FCL, LCL, CLX with 210 examples and 9 tutorials (see in the file: upsi_allfunctionslist.txt) start from a USB stick or from a UNC Network Path # new output menu of styles / screenshot for prototyping or teaching incl. context menu # include kernel functions of compiler and makro editor with RegEX2 # new Units: DB System, Tables and DataSets, Printer, MediaPlayer, Grids, Clipboard, DBX, ADO, JUtils, XML, FileUtils This Version is shipping on April 2011 http://www.softwareschule.ch/maxboxnews.htm - PNG, TIFF, JPG and more graphics support for Canvas and TPicture - SMTP, POP3, HTTP, FTP, sysutils, strutils, shell and ini support - Improvments of 64Bit, PNG, and Ansi/WideStrings are done - Command Line Interface. The mX3 is fully command line enabled for both the Win and Linux system, see example 150_command_line.