No announcement yet.

Restarting a program...

  • Filter
  • Time
  • Show
Clear All
new posts

  • Restarting a program...

    ...without exiting and clicking the icon again.

    I'm writing a program and there are occasions when I need to clear all variables and start the program fresh.

    Under PB/DOS, all I needed to do was CLEAR and RUN. Can't do that in CC.

    I've looked the help files and the forums but can't seem to find what I'm looking for or maybe I'm just looking for the wrong key word(s).

    Any help out there?
    There are no atheists in a fox hole or the morning of a math test.
    If my flag offends you, I'll help you pack.

  • #2
    I've done it with the SHELL function:
    Run an executable program asynchronously (as a separate process), while execution of the original application continues uninterrupted
    Then exit the 1st program normally.


    • #3
      The program might need a moment for the disk cache to be flushed etc. You could start a batch file that pauses x seconds (duration taken as a command line parameter) then calls your program. Or call another instance of your program and use a parameter to specify a sleep duration. After either, exit the original.

      I would really prefer PB provide a restart mechanism. PB can then keep a certain amount of memory allocated to guarantee the swap file cannot shrink and regrow between the exit and restart.
      Erich Schulman (KT4VOL/KTN4CA)
      Go Big Orange


      • #4
        Well, if you really want to 'wait for something' before allowing the new instance of the program to start actually doing something...

        .. as soon as you see "wait for something" you should be thinking synchronization objects, not timers. (Didn't we just have this discussion here?)

        Have your program as its first step wait on obtaining ownership of a named mutex or semaphore... the new instance cannot obtain that ownership until the prior instance has completely ended and the O/S has deleted the object by closing all its open handles.

        (I like the semaphore here... no creation/destruction timing issues to deal with).

        Memory is not an issue... each new process gets its own 2 Gb user space, and as long as you have sufficient RAM+free disk the new instance will start just fine.

        Trying to mess with the swap file is a fool's errand... let Windows handle it, that's what operating systems are designed to do.


        (Afterthought: Wouldn't this make a great demo for semaphores, where we currently have no demos? I think I'll do that).
        Last edited by Michael Mattias; 2 Apr 2008, 10:55 AM.
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]


        • #5
          Well, I was working on the semaphore version of this over the weekend.

          I got it to work for this application, but I decided that using a semaphore for this application was silly and a mutex would be superior.

          So let me change my recommendation to "I really like the mutex for this application."

          FWIW, it's pretty simple..
          $MUTEX_NAME = "something"
          FUNCTION PbMain() AS LONG
            hMutex  = CreateMutex(..._, $MUTEX_NAME) 
            iWait = WaitForsingleObject (hMutex, %INFININTE)
            IF iWait = %WAIT_OBJECT_0 THEN
               CALL MainProcedure  (params, bRestart)  ' set restart flag in this proc if wanted
               IF ISTRUE bRestart THEN
                   ' execute ourself ansynchronously
                   GetModuleFileName BYVAL %NULL, szProgram, SIZEOF(szProgram) 
                   X = SHELL (szProgram) 
              END IF
            ' release ownership of the mutex
             ReleaseMutex hMutex
            ' handle WFSO errors 
           END IF
          ' --------------------------------------------------------------
          ' Two schools of thought here at the end of WinMain:  
          ' A. Thou shalt release that which you obtain
          ===>   CloseHandle hMutex
          ' B. Allow Windows to close the process' open handle to the mutex, since 
          ' it will only do so when the current process is TOTALLY "ended" so the new instance
          ' of the program can only obtain ownership when the the Wicked Witch of the West 
          ' is "really, really, truly dead"
          ==>  DO NOTHING ====
          END FUNCTION  ' Pbmain
          Oh, well, at least the exercise gave me some ideas for a decent semaphore demo...

          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]


          • #6

            Somtimes can be used
            /Fim W
            Fim Wästberg