Announcement

Collapse
No announcement yet.

Restart - Random Fail

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • #21
    Mike,
    An END before an END FUNCTION is an unusual combination. Can you expand on how the END might affect the result?

    Especially, since Help says ...

    ... you should eliminate it as soon as possible."
    ... I might never have considered using it.

    Comment


    • #22
      Mike,
      All those reservations said, I went ahead and tried it on another program of mine, where I use the restart method.

      In that case, when the app restarts, it did not always receive focus, regardless of my code attempts to make sure it did get focus.

      With END used, the app always gets focus when it restarts. It is repeatable.

      Comment


      • #23

        Code:
        FUNCTION PBMAIN() AS LONG
         LOCAL hDlg AS DWORD
         DIALOG NEW PIXELS, 0, "F1 to Auto Restart",,,300,50, %WS_OVERLAPPEDWINDOW TO hDlg
         DIALOG SHOW MODAL hDlg CALL DlgProc
        END FUNCTION
        
        CALLBACK FUNCTION DlgProc
         SELECT CASE CBMSG
          CASE %WM_HELP:DIALOG END CB.HNDL:RestartProgram
         END SELECT
        END FUNCTION
        
        FUNCTION RestartProgram AS LONG
         LOCAL pid AS LONG
         'cleanup optional close files, signal threads to finish, etc
         pid = SHELL(EXE.FULL$, 1)
         END 'assure no other code executes and no race condition
        END FUNCTION
        https://duckduckgo.com instead of google

        Comment


        • #24

          pid=SHELL(EXE.FULL$,1):END .....focus? ..........

          > With END used always gets focus
          That should be correct since only the new program can get focus.
          Works?

          >All those reservations said, I went ahead and tried it on another program of mine, where I use the restart method.
          >In that case, when the app restarts, it did not always receive focus, regardless of my code attempts to make sure it did get focus.
          Doesn't work?

          programA 'program is running
          'request to terminate could be at any moment (anywhere in the program)
          shellexecute 'programA started again (race condition)
          end 'end immediately so only 1 copy will be running

          Does new program get focus or the desktop? (original program should have ended)
          https://duckduckgo.com instead of google

          Comment


          • #25
            Hi Mike,

            Sorry for not being more clear.

            What worked was that using END solved a different problem I was having, where an app of mine would not regain focus after closing and restarting in the same manner as discussed in the OP. In that case, when I placed END after SHELL, the restarted app always gained focus as intended. It's repeatable but I still don't understand why that helped.

            I've not been able to test the OP code with END on my daughter's PC. The problem there was that the app would not restart at all. She's just not been available to try it out.

            Comment


            • #26
              Sounds like original program had focus when copy started then lost focus to desktop.
              https://duckduckgo.com instead of google

              Comment


              • #27
                I don't care for using END

                Reading post #5 downloading and renaming is involved
                This kills .old file (if needed), renames original, renames back, tests threadcount=1 and close files before shell
                Why shelled to program doesn't always get focus or random fail?
                Code:
                'Compilable Example:
                #COMPILE EXE
                #DIM ALL
                %Unicode=1
                #INCLUDE "Win32Api.inc"
                GLOBAL hDlg,pid,Restart AS DWORD
                
                FUNCTION PBMAIN() AS LONG
                   LOCAL sOriginal,sRenamed AS STRING
                   sOriginal = EXE.FULL$
                   sRenamed = sOriginal + ".old"
                   DIALOG NEW PIXELS, 0, "F1 to Auto Restart",,,300,50, %WS_OVERLAPPEDWINDOW TO hDlg
                   DIALOG SHOW MODAL hDlg CALL DlgProc
                
                   IF Restart THEN
                    IF ISFILE(sRenamed) THEN ? "Attempt to kill" + sRenamed:KILL sRenamed
                    IF ERR THEN ? USING$("Could not delete & error #",sRenamed,ERRCLEAR):GOTO ExitPBMain
                    NAME sOriginal AS sRenamed
                    IF ERR THEN ? USING$("Could not rename & error #",sOriginal,ERRCLEAR):GOTO ExitPbMain
                    NAME sRenamed AS sOriginal  'would be successful download
                    IF ERR THEN ? USING$("Could not rename & error #",sRenamed,ERRCLEAR):GOTO ExitPbMain
                    IF THREADCOUNT <> 1 THEN ? "Something is still running in the background"
                    CLOSE
                    pid = SHELL(sOriginal, 1) 'Rerun this app
                   END IF
                
                ExitPbMain:
                END FUNCTION
                
                CALLBACK FUNCTION DlgProc
                   SELECT CASE CBMSG
                      CASE %WM_HELP : Restart = 1 : DIALOG END hDlg
                   END SELECT
                END FUNCTION
                https://duckduckgo.com instead of google

                Comment


                • #28
                  Kerry,

                  After all that discussion, I wound up doing what you suggested in post #2.

                  But the work around is to shell out to another program which SLEEPS for a bit and then shells back to the original program. This gives the original program time to close nicely and then be reopened again.
                  I had other fish to fry and decided to go ahead with something that seemed most likely to work (and it does, here and on my daughter's PC), rather than think more about the original problem. Maybe when I retire ...

                  Comment


                  • #29
                    Gary,

                    Why wait to shut the first one down, start a new instance THEN shut the first one down.
                    hutch at movsd dot com
                    The MASM Forum

                    www.masm32.com

                    Comment


                    • #30
                      Originally posted by Gary Beene View Post
                      Kerry,

                      After all that discussion, I wound up doing what you suggested in post #2.
                      .
                      Wow Gary - to think I helped a guru like you - Wow
                      [I]I made a coding error once - but fortunately I fixed it before anyone noticed[/I]
                      Kerry Farmer

                      Comment


                      • #31
                        Hi Steve!
                        In this case, my app doesn't allow a second instance, but I could temporarily override that restriction, do what you say, and then restore the constraint.

                        The unusual circumstances in which the existing code failed drew me like a moth to a light. I think many of the responses may have come close to the answer but an exact/demonstrable explanation still eludes me. I've been known to rub up against a problem till my fur drops off, but in this case I just went with the simple solution and got on with other tasks.

                        Yo Kerry!
                        Everyone has something to offer and I'm happy to take knowledge from any and all sources!. Or, at my age, re-learn something every day. I originally avoided the 2nd EXE solution just because I thought the restart approach was simple and foolproof. In over a decade, it never failed me once - until recently when my daughter had problems with it on her PC.

                        Comment


                        • #32
                          Kerry,
                          On the Guru topic ... I'm an engineer by training and I always think of the brains of this outfit as the folks like Jose, Pierre, Steve, Dominic, Paul, Dave, Dominic ... the like .. who know topics to a detail that usually escapes me. Engineers love building stuff, which is how I think of my coding. I'm a user of concepts that are mostly developed by other folks, and am happy to have such work available to me! It's like having the biggest Tinker-Toy set ever!

                          Comment


                          • #33
                            There is an even simpler way if you don't mind the crudity, exit after writing a batch file which re-starts the app that has just shut down and when the app starts, search for the batch file and delete it if it exists. You would have to have a separate way to shut it down.
                            hutch at movsd dot com
                            The MASM Forum

                            www.masm32.com

                            Comment


                            • #34
                              Just when you thought this thread had finished.

                              Lance Edmonds had a program named WinLink that would probably do what you want.
                              The batch file method should also work, haven't tried.

                              How about CreateProcess (which allows more options and I believe passing much more information if needed?)
                              Working drive and directory were kludged since they could have been part of the structure.

                              Code:
                              #INCLUDE "win32api.inc"
                              
                              FUNCTION PBMAIN () AS LONG 'createprocesstest.bas
                                 LOCAL sProgramname AS STRING,ShowWind,TimeLimit,ExitCode,result AS LONG
                                 sProgramName = EXE.FULL$
                                 ShowWind = %SW_SHOWNORMAL
                                 TimeLimit = 0
                                 result = MSGBOX("Restart?",%MB_YESNO OR %MB_SYSTEMMODAL,sProgramName)
                                 IF result = %IDYES THEN
                                    ExitCode = Waitshell(sProgramName, ShowWind,TimeLimit,ExitCode)
                                    REM ? STR$(ExitCode),%MB_SYSTEMMODAL,"ExitCode"
                                 END IF
                              END FUNCTION
                              
                                 '------------------------------------------------------------------------------------
                              FUNCTION WaitShell(BYVAL ProgramName AS STRING, _
                                                 BYVAL ShowWind    AS LONG  , _
                                                 BYVAL TimeLimit   AS LONG ,  _
                                                 ExitCode    AS LONG) AS LONG
                               REM Success = 1
                              
                               'IF UCASE$(ProgramName) = UCASE$(EXE.FULL$) OR UCASE$(programname) = UCASE$(EXE.NAME$)THEN
                               '  ? "Restarting yourself could cause an endless loop!" + $CR + "Please use another function.",%MB_ICONERROR,LCASE$(ProgramName)
                               '  FUNCTION = -99
                               '  EXIT FUNCTION
                               'END IF
                               'Execute program or batch file and set working drive/directory
                               'Returns:
                               '0 = success
                               'negative number = Windows System Error Message
                               'positive number = PowerBASIC Error Message
                               '
                                LOCAL Si AS STARTUPINFO
                                LOCAL Pi AS PROCESS_INFORMATION
                                LOCAL sCurrentDIR   AS STRING
                                LOCAL sCurrentDRIVE AS STRING
                                LOCAL sWorkingDrive AS STRING
                                LOCAL sWorkingDir   AS STRING
                                LOCAL EndOfPathPosition AS LONG
                                LOCAL Result        AS LONG
                                '
                                Si.cb = SIZEOF(Si)
                                Si.dwFlags = %STARTF_USESHOWWINDOW
                                Si.wShowWindow = ShowWind&
                                'Drive for new process
                                IF MID$(ProgramName,2,1) = ":" THEN
                                  sWorkingDrive = LEFT$(ProgramName,1)
                                  CHDRIVE sWorkingDrive
                                  IF ERR THEN
                                    'invalid drive
                                     FUNCTION = ERR
                                     GOTO WaitShellExit
                                  END IF
                                END IF
                                '
                                'Directory for new process
                                EndOfPathPosition = INSTR(-1,ProgramName,"\")
                                IF EndOfPathPosition THEN   'we have a path
                                 IF LEN(sWorkingDrive) = 0 THEN
                                   sWorkingDir = LEFT$(ProgramName,EndOfPathPosition)
                                 ELSE
                                   sWorkingDir  = MID$(ProgramName,3,EndOfPathPosition-3)
                                 END IF
                                 CHDIR sWorkingDir
                                 IF ERR THEN
                                   FUNCTION = ERR
                                   GOTO WaitShellExit
                                 END IF
                                END IF
                                result = CreateProcess("", _
                                     BYVAL STRPTR(ProgramName), _
                                     BYVAL %NULL, _
                                     BYVAL %NULL, _
                                     0, _
                                     %NORMAL_PRIORITY_CLASS, _
                                     BYVAL %NULL, _
                                     BYVAL %NULL, _
                                     Si, _
                                     Pi)
                                IF result THEN
                                  'CALL WaitForInputIdle(pi.hProcess, %INFINITE)
                                  CALL WaitForSingleObject(pi.hProcess, TimeLimit&)
                                  GetExitCodeProcess(pi.hProcess,ExitCode)
                                  CALL CloseHandle(pi.hProcess)
                                  CALL CloseHandle(pi.hThread)
                                  FUNCTION = result
                                ELSE
                                  FUNCTION = -GetLastError
                                END IF
                              WaitShellExit:
                              END FUNCTION
                              https://duckduckgo.com instead of google

                              Comment


                              • #35
                                Gary,

                                Here is a working test piece that does the job. If you are using DDT dialogs you will need to change the FindWindow code to what works with DDT but this code does what it is supposed to, does not allow duplicates AND can restart itself. Full source is in the zip.

                                In PBmain.
                                Code:
                                  ' |||||||||||||||||||||||||||||||||||||||||||||
                                
                                    thiswin = FindWindow(szClassName,ByVal %NULL)
                                
                                    If thiswin <> 0 Then
                                      SetForegroundWindow thiswin
                                      Exit FUNCTION
                                    End If
                                
                                  ' |||||||||||||||||||||||||||||||||||||||||||||
                                In WndProc.
                                Code:
                                        ' ||||||||||||||||||||||||||||||||||||||||||||||||||
                                
                                          Case 51
                                            xx& = shell(ThisApp$,1)
                                            SendMessage hWin,%WM_SYSCOMMAND,%SC_CLOSE,0
                                
                                            SleepEx 10,0
                                
                                            thiswin = FindWindow(szClassName,ByVal %NULL)
                                            SetForegroundWindow thiswin
                                
                                          Case 52
                                            SendMessage hWin,%WM_SYSCOMMAND,%SC_CLOSE,0
                                
                                        ' ||||||||||||||||||||||||||||||||||||||||||||||||||
                                Attached Files
                                hutch at movsd dot com
                                The MASM Forum

                                www.masm32.com

                                Comment


                                • #36
                                  Hey Gary,

                                  Looking at alternative ways to self restart is fine, still, before that,
                                  identifying what is going wrong on your daughter's PC seems to me pretty important.
                                  It might be a problem that will have many other influences, it may bite back in the future...

                                  If I may ask, did you have the chance to run some debugging code on her pc that could show the errors encountered?
                                  Last edited by Pierre Bellisle; 19 Feb 2019, 12:15 PM.

                                  Comment


                                  • #37
                                    Originally posted by Gary Beene View Post
                                    Kerry,
                                    On the Guru topic ... I'm an engineer by training and I always think of the brains of this outfit as the folks like Jose, Pierre, Steve, Dominic, Paul, Dave, Dominic ... the like .. who know topics to a detail that usually escapes me. Engineers love building stuff, which is how I think of my coding. I'm a user of concepts that are mostly developed by other folks, and am happy to have such work available to me! It's like having the biggest Tinker-Toy set ever!
                                    I am a mathematician by education and a very old fashioned programmer by trade. I remain totally amazed at the knowledge - breadth and depth - of the team on this forum. And I too like the Tinker-Toy (which forgive me, I would probably say 'Mechano') concept.
                                    [I]I made a coding error once - but fortunately I fixed it before anyone noticed[/I]
                                    Kerry Farmer

                                    Comment


                                    • #38
                                      NOTE:
                                      I had the same issue using a batch file to restart my app. The solution was to make sure the batch file was completely closed before calling it with ShellExecute.

                                      Code:
                                        local zExeName      AS ASCIIZ * %MAX_PATH
                                        LOCAL zBatName      AS ASCIIZ * %MAX_PATH
                                        LOCAL hFile         AS DWORD
                                      
                                        hFile = FREEFILE
                                        zExeName = "YourAppName.exe"
                                      
                                      
                                        if giWantToDoAReboot = 1 then
                                            '? "reboot here"
                                            'Create a bat file that will restart exe
                                            zBatName = "REBOOT.BAT"
                                            OPEN zBatName FOR OUTPUT AS hFile
                                            PRINT #hFile, "Start " & $DQ & "Restarting exe" & $DQ & $SPC & $DQ & zExeName & $DQ 'Restart the exe
                                            CLOSE hFile
                                      sleep 300    'sleep is important here (could check to make sure BAT file is closed before continuing)
                                            ShellExecuteA(0, "Open", zBatName, "", "", 0) 'Run the newly created batch file
                                        end if

                                      Comment


                                      • #39
                                        This line at top of batch file will change directory to where batch file is located.
                                        This may be important when executing batch files in other folders.
                                        Code:
                                        cd /d %~dp0
                                        https://duckduckgo.com instead of google

                                        Comment


                                        • #40
                                          I put a 2 second wait function (TIMEOUT) in the batch file. Seemed to fix the problem. Time will tell.

                                          Code:
                                            local zExeName      AS ASCIIZ * %MAX_PATH
                                            LOCAL zBatName      AS ASCIIZ * %MAX_PATH
                                            LOCAL hFile         AS DWORD
                                          
                                            hFile = FREEFILE
                                            zExeName = "YourAppName.exe"
                                          
                                            if giWantToDoAReboot = 1 then
                                                '? "reboot here"
                                                'Create a bat file that will restart exe
                                                zBatName = "REBOOT.BAT"
                                                OPEN zBatName FOR OUTPUT AS hFile
                                                PRINT #hFile, "timeout /t 2 /nobreak > NUL"    'HAD TO PUT IN A WAIT FUNCTION
                                                PRINT #hFile, "Start " & $DQ & "Restarting exe" & $DQ & $SPC & $DQ & zExeName & $DQ 'Restart the exe
                                                CLOSE hFile
                                          
                                                ShellExecuteA(0, "Open", zBatName, "", "", 0) 'Run the newly created batch file
                                            end if
                                          Last edited by Jim Fritts; 10 May 2019, 05:21 PM.

                                          Comment

                                          Working...
                                          X