App Termination and Relaunch.

App Termination and Relaunch.

Postby byron.hopp » Fri Oct 06, 2023 6:44 pm

Is there an easy way to terminate my app then immediately relaunch it. Obviously this is a workaround for a problem the app has but I need an immediate solution while trying to figure out what the main problem is. Thanks,
Thanks,
Byron Hopp
Matrix Computer Services
byron.hopp
 
Posts: 382
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA

Re: App Termination and Relaunch.

Postby Antonio Linares » Fri Oct 06, 2023 6:52 pm

Dear Byron,

You may try:

ShellExecute( 0, "open", "yourapp.exe" )
__Quit()
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: App Termination and Relaunch.

Postby Antonio Linares » Fri Oct 06, 2023 6:53 pm

Yes, in Windows programming, you can terminate your application and then immediately relaunch it. There are several ways to achieve this, but one common approach is to use the `ShellExecute` function or the `CreateProcess` function to start a new instance of your application from within your existing application. Here's a general outline of how you can do it:

1. Terminate the Current Instance:
You can use the `ExitProcess` function or simply return from the `main` function (if you are using C/C++), or use the appropriate method in your programming language to gracefully terminate your application.

2. Relaunch the Application:
After terminating the current instance, you can use one of the following methods to relaunch your application:

a. `ShellExecute`:
```cpp
#include <Windows.h>

int main() {
// Terminate the current instance

// Relaunch the application
ShellExecute(NULL, L"open", L"your_app.exe", NULL, NULL, SW_SHOWNORMAL);

return 0;
}
```
In this example, replace `"your_app.exe"` with the path to your application's executable.

b. `CreateProcess`:
```cpp
#include <Windows.h>

int main() {
// Terminate the current instance

// Relaunch the application
STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi;
if (CreateProcess(NULL, L"your_app.exe", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}

return 0;
}
```
Again, replace `"your_app.exe"` with the path to your application's executable.

Depending on your specific requirements, you may need to pass command-line arguments or adjust other parameters when relaunching your application.

Remember that this approach will completely terminate the current instance of your application and launch a new one. If you need to perform some specific tasks or maintain state between the termination and relaunch, you'll need to implement those as part of your application logic.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: App Termination and Relaunch.

Postby Antonio Linares » Fri Oct 06, 2023 6:54 pm

You can also achieve the termination and relaunch of your application using a batch file in Windows. Here's a simple example of how you can create a batch file to do this:

1. Create a text file with a `.bat` extension, such as `restart_my_app.bat`.

2. Edit the batch file using a text editor like Notepad, and add the following commands:

```batch
@echo off
taskkill /F /IM your_app.exe
start "" "C:\path\to\your_app.exe"
```

Replace `"C:\path\to\your_app.exe"` with the actual path to your application's executable.

Here's what these commands do:

- `@echo off`: This command turns off the command echoing in the batch file, so the commands themselves won't be displayed on the console.

- `taskkill /F /IM your_app.exe`: This command forcefully terminates any running instances of your application named `your_app.exe`. Replace `your_app.exe` with the actual name of your executable.

- `start "" "C:\path\to\your_app.exe"`: This command starts a new instance of your application. Replace `"C:\path\to\your_app.exe"` with the actual path to your application's executable. The `""` is used as a placeholder for the window title.

Save the batch file after editing.

3. To restart your application, simply double-click the batch file (`restart_my_app.bat`) whenever you want to terminate the current instance and start a new one.

Keep in mind that using a batch file in this way will forcibly terminate the existing instance of your application and then start a new one. If your application has any unsaved data or requires specific cleanup procedures before termination, you should implement those in your application code or a separate script that the batch file can call before terminating the application.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: App Termination and Relaunch.

Postby Natter » Sat Oct 07, 2023 9:50 am

You can do this: Run an external program from your program -upg.exe ()

Code: Select all  Expand view
winexec("upg", 1)
quit


From your program upg.exe () you launch your application, and upg.ехе closing
Code: Select all  Expand view
#INCLUDE "FiveWin.ch"

function Main
   millisec(50)
  winexec(MyApp, 1)
  quit
return NIL
Natter
 
Posts: 1221
Joined: Mon May 14, 2007 9:49 am

Re: App Termination and Relaunch.

Postby karinha » Sat Oct 07, 2023 4:02 pm

Simples,

Code: Select all  Expand view

// C:\FWH..\SAMPLES\TUTOR09.PRG
// Modificado en: 07/10/2023

#Include "FiveWin.ch"
#Include "Directry.ch"

#Define aPubGrad {| lInvert | If( lInvert, ;
                 { { 1 / 3, nRGB( 255, 253, 222 ), nRGB( 255, 231, 151 ) }, ;
                 { 2 / 3, nRGB( 255, 215,  84 ), nRGB( 255, 233, 162 ) }    ;
                 },                                                         ;
                 { { 1 / 2, nRGB( 219, 230, 244 ), nRGB( 207 - 50, 221 - 25, 255 ) }, ;
                 { 1 / 2, nRGB( 201 - 50, 217 - 25, 255 ), nRGB( 231, 242, 255 ) }    ;
                 } ) }

STATIC oWnd
STATIC lSalida := .F.
STATIC oFont, nHFont, cFont, nWFont, cFontH, oBrush, oFnt, oFont40

MEMVAR cDirPleno

FUNCTION Main()

   LOCAL obar, oBmp, oIco

   PRIVATE cDirPleno

   HB_GCALL( .F. )

   SET CENTURY ON
   SET DATE BRITISH
   SET TIME FORMAT TO "HH:MM:SS"
   SET EPOCH TO YEAR( DATE() ) - 30
   SET SOFTSEEK OFF
   SET WRAP ON
   SETCANCEL( .F. )
   SET CONFIRM OFF
   SET DELETED ON
   SET _3DLOOK ON
   SET UNIQUE OFF
   SET ESCAPE OFF
   SET EXACT ON
   SET EXCLUSIVE OFF
   SET MULTIPLE OFF
   SET OPTIMIZE ON

   SetBalloon( .T. )

   SkinButtons()

   cDirPleno := GETCURDIR()

   IF SUBS( cDirPleno, LEN( ALLTRIM(cDirPleno ) ) , 1 ) = "\"
      cDirPleno := SUBS( cDirPleno, 1 , LEN( ALLTRIM(cDirPleno ) ) - 1 )
   ENDIF

   LCHDIR( cDirPleno )

   cFont   := "
Segoe UI Symbol"  //"Calibri"
   cFontH  := -16  //-10 //-12 //-16 //-18  //-20

   IF !IsWin8() .and. !IsWindows10()
      cFont   := "
Calibri"   //"Tahoma"
   ENDIF

   DEFINE FONT oFont   NAME cFont    SIZE 0, cFontH WEIGHT 300
   DEFINE FONT oFont40 NAME 'Tahoma' SIZE 0, -40 BOLD
   DEFINE ICON oIco FILE "
..\icons\fax.ico"

   DEFINE WINDOW oWnd FROM 1, 1 TO 22, 75 TITLE "
Sistema Generico "  ;
      MENU BuildMenu() ICON oIco NOSYSMENU

   DEFINE BUTTONBAR oBar _3D SIZE 40, 40 OF oWnd 2007

   oBar:bClrGrad := aPubGrad

   oBar:bRClicked := { || ( NIL ) } // Mouse Direito
   oBar:bLClicked := { || ( NIL ) } // Mouse Esquerdo

   oBar:nClrText := CLR_BLACK
   oBar:Adjust()

   oBar:SetFont( oFont )

   DEFINE BUTTON OF oBar FILENAME "
..\bitmaps\16x16\new.bmp" FLAT ;
      ACTION MsgInfo( "
New" ) ;
      TOOLTIP "
Creates a new document" PROMPT "New" GROUP

   DEFINE BUTTON OF oBar FILENAME "
..\bitmaps\16x16\open.bmp" FLAT ;
      ACTION MsgInfo( cGetFile( "
*.*", "Select a document to open" ) ) ;
      TOOLTIP "
Opens a document" WHEN .f. PROMPT "Open" GROUP

   DEFINE BUTTON OF oBar FILENAME "
..\bitmaps\16x16\floppy.bmp" FLAT ;
      ACTION MsgInfo( Time() ) TOOLTIP "
Saves this document" PROMPT "Doc" GROUP

   DEFINE BUTTON OF oBar FILENAME "
..\bitmaps\16x16\printer.bmp" FLAT ;
      ACTION MsgInfo( "
Prints this document" ) TOOLTIP "Print this document" ;
      GROUP PROMPT "
Print"

   DEFINE BUTTON OF oBar FILENAME "
..\bitmaps\16x16\prop.bmp" FLAT ;
      ACTION PrinterSetup() TOOLTIP "
Setup the printer" PROMPT "Prop" GROUP

   DEFINE BUTTON OF oBar FILENAME "
..\bitmaps\16x16\HelpInd.bmp" FLAT ;
      ACTION MsgInfo( Version() ) TOOLTIP "
A multiple lines" + ;
      Chr( 13 ) + Chr( 10 ) + "
tooltip!" GROUP PROMPT "Help"

   DEFINE BUTTON OF oBar FILENAME "
..\bitmaps\16x16\Help.bmp" FLAT ;
      ACTION ( lSalida := .T., DESLIGA_PROGRAM_SYS() )             ;
      TOOLTIP "
Shows program shutdown."                            ;
      PROMPT "
shutd" GROUP

   DEFINE BUTTON OF oBar FILENAME "
..\bitmaps\16x16\Exit.bmp" FLAT ;
      ACTION( oWnd:End(), CERRAR_TODO( lSalida := .T. ) )          ;
      TOOLTIP "
Exit this app" GROUP                                ;
      PROMPT "
Exit"

   SET MESSAGE OF oWnd TO "
© kapiabafwh@gmail.com - Skype: joao@pleno.com.br" ;
       NOINSET CLOCK DATE KEYBOARD COLOR CLR_HBLUE, CLR_WHITE 2007 FONT oFont

   DEFINE BITMAP oBmp FILENAME "
..\bitmaps\sea.bmp"

   oWnd:bPainted = { | hDC | BmpTiled( hDC, oWnd, oBmp ) }

   ACTIVATE WINDOW oWnd MAXIMIZED ;
      VALID( lSalida )

RETURN NIL

FUNCTION BuildMenu()

   local oMenu

   MENU oMenu
      MENUITEM "
Information"
      MENU
         MENUITEM "
&About..." ;
            ACTION MsgInfo( FWDESCRIPTION ) ;
            FILENAME "
..\bitmaps\16x16\info.bmp"

         SEPARATOR
         MENUITEM "
&Shutdown" ;
            ACTION ( lSalida := .T., DESLIGA_PROGRAM_SYS() )     ;
            FILENAME "
..\bitmaps\16x16\Help.bmp"

         SEPARATOR
         MENUITEM "
&End..."  ;
            ACTION( oWnd:End(), CERRAR_TODO( lSalida := .T. ) ) ;
            FILENAME "
..\bitmaps\16x16\exit.bmp"

      ENDMENU

      MENUITEM "
&Clients"
      MENU
         MENUITEM "
&New..." ;
            ACTION ( MsgStop( "
New Clients" ),;
                     oWnd:Say( 5, 5, "
New Clients...", "GR+/G" ) ) ;
            FILENAME "
..\bitmaps\16x16\faces.bmp"

         MENUITEM "
&Modify..."  ACTION MsgInfo( "Modif. Clients" ) ;
            FILENAME "
..\bitmaps\edit.bmp"

         MENUITEM "
&Delete..."  ACTION MsgAlert( "Del Clients" ) ;
            FILENAME "
..\bitmaps\16x16\delete.bmp"

         SEPARATOR

         MENUITEM "
&Browse..."  ACTION MsgInfo( "Browse Clients" ) ;
            FILENAME "
..\bitmaps\16x16\browse.bmp"

      ENDMENU

      MENUITEM "
&Utilities"
      MENU
         MENUITEM "
&Calculator..." ACTION WinExec( "Calc" ) ;
            FILENAME "
..\bitmaps\16x16\calc.bmp"

         MENUITEM "
&Internet..." ;
            ACTION WinExec( "
start iexplore www.fivetech.com", 0 ) ;
            FILENAME "
..\bitmaps\16x16\explorer.bmp"
      ENDMENU
   ENDMENU

RETURN( oMenu )

FUNCTION CERRAR_TODO()

   IF FILE( "
TUTOR09.LOG" )
      DELETEFILE( "
TUTOR09.LOG" )
   ENDIF

   lSalida := .F.

   DbCommitAll()
   DbUnLockAll()
   DbCloseAll()
   FreeResources()
   Release All
   SysRefresh()

   HB_GCALL( .T. )

   CLEAR MEMORY

   PostQuitMessage( 0 )

   QUIT

RETURN NIL

STATIC FUNCTION BmpTiled( hDC, oWnd, oBmp )

   local nWidth := oWnd:nWidth(), nHeight := oWnd:nHeight()
   local nRow := 0, nCol := 0, n
   local nBmpWidth  := oBmp:nWidth(),  nBmpHeight := oBmp:nHeight()

   if oBmp:hBitmap == 0
      return nil
   endif

   while nRow < nHeight
      nCol = 0
      while nCol < nWidth
         PalBmpDraw( hDC, nRow, nCol, oBmp:hBitmap )
         nCol += nBmpWidth
      end
      nRow += nBmpHeight
   end

RETURN NIL
// TURN OFF SYSTEM-PROGRAM
FUNCTION DESLIGA_PROGRAM_SYS()  // 07/10/2023 - Joao

   LOCAL Nome_Arq, nRegistro, oDlg, cMsg, cTexto

   MsgRun( "
Desligando o Programa...           ", ;
           "
Por Favor, Espere!                 ", ;
           { | oDlg | ( DESLIGA_PROG_TUTOR09( oDlg ) ) } )

   // AEVAL(DIRECTORY( "
TUTOR09.BAT"     ),{ |aFILE| FERASE(aFILE[F_NAME]) } )

   IF .NOT. FILE( "
TUTOR09.BAT" )

      NOME_ARQ := FCREATE("
TUTOR09.BAT")

      NREGISTRO := "
@ECHO OFF"                                          ;
                   + CRLF        +                                      ;
                   "
CLS"         +                                      ;
                   + CRLF + CRLF +                                      ;
                   cDirPleno + "
\TUTOR09 > NUL"  +                      ;
                   + CRLF + CRLF +                                      ;
                   "
CLS"         +                                      ;
                   + CRLF        +                                      ;
                   "
EXIT"

      FWRITE( NOME_ARQ, NREGISTRO )
      FCLOSE( NOME_ARQ )

   ENDIF

   lSalida := .F.

   WinExec( "
TUTOR09.BAT", 0 )

   FreeResources()
   SysRefresh()
   DbCloseAll()

   RELEASE ALL
   HB_GCALL( .T. )

   CLEAR MEMORY

   PostQuitMessage( 0 )

   __QUIT()

RETURN NIL
//-> Shows program shutdown.
FUNCTION DESLIGA_PROG_TUTOR09( oDlg )

   LOCAL nFor, nStart

   FOR nFor := 4 to 0 step -1
       nStart = GetTickCount()
       while ( GetTickCount() - nStart ) < 1000
       end
       oDlg:cMsg := "
Aguarde, Desligando o Programa em: "+ ;
                     LTrim( Str( nFor ) ) + "
Segundo"   + ;
                     If( nFor > 1, "
s", "" )

       oDlg:Refresh()
       SysRefresh()

   NEXT

RETURN NIL

/*
// TUTOR09.BAT
@ECHO OFF
CLS

c:\FWH1905\samples\TUTOR09 > NUL

CLS
EXIT
*/

// FIN / END - kapiabafwh@gmail.com


Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7826
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 55 guests