Announcement

Collapse
No announcement yet.

Force Close

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

  • Force Close

    Is there a way to automatically close out all users from a program at a specific time? My program is accessed by numerous users in three plants. Humans being what they are, it isn't unusual for someone to leave at the end of the day without closing the program. Hence, if I need to make a program change afterhours, I cannot because of a sharing instance. I need an ingenious way to shut them down either by intervention or a timer after a certain hour. They never work at night, so that would let me do my updates. Thanks for any ideas.

  • #2
    Yes, there are several ways. It's trivial to set up a simple timer that checks the system time every minute of so and closes the application if it is after a cut off time.

    But it's a generally a very bad idea to forcible shut down an application, especially if it may have files or database connections open ( and Murphy's Law says that one day, someone WILL be working late )

    A better approach is to not have multiple users share the application executable. One alternative is to start the application through a batch file or simple "stub" application that copies the latest version from a repository to a local directory and then runs the copy.

    That way you can roll out an update at any time with no problems, not just after hours.

    You can refine it so that the stub checks the version in the repository and only copies it to the local directory if it is more recent that the existing local copy.
    You can even have your application check periodically to see if there is a new version and prompt the user to restart (thereby loading the new version).

    Comment


    • #3
      Thank you Stuart! Sounds like a great solution.

      Comment


      • #4
        That happens to me a lot as well. As long as it is just the application I go to the server and close the share. Seems brutal but have never been burned by it. Never could figure why it would lock up the .EXE file after it is read into the users computer. Seems like a useless lock.

        Comment


        • #5
          I've just knocked up a quick demo of running the latest networked version locally:
          https://forum.powerbasic.com/forum/u...lly#post802788

          Added: I've just updated the source to include Version checking.

          Comment


          • #6
            To see Windows shutdown options:
            At a command prompt:
            type shutdown /? > me.txt
            type me.txt

            This gives a user an option to prevent shutdown.
            Checks time every 60000-milliseconds to see if => $SHUT_DOWN_TIME.
            Code:
            $SHUT_DOWN_TIME = ""  '20:00 needs work if after midnight
            FUNCTION PBMAIN () AS LONG
             THREAD CREATE Something(0) TO h&:THREAD CLOSE h& TO h&
             ? FUNCNAME$
            END FUNCTION
            
            THREAD FUNCTION Something(BYVAL NotUsed AS LONG)
             DO
              IF TIME$ >= $SHUT_DOWN_TIME THEN
               pid& = SHELL("\windows\system32\shutdown.exe /s /t 120  /c " + """Please click CLOSE then Ok within 120-seconds to prevent shut-down!",0)
               SLEEP 4000
               ? "Please click Ok to prevent shut-down",%MB_ICONERROR OR %MB_SYSTEMMODAL,"Abort shut-down"
               pid& = SHELL("\windows\system32\shutdown.exe /a",0) 'cancel shutdown
               EXIT FUNCTION  'don't check any more, user is present
              END IF
              SLEEP 60000 'check every minute
             LOOP
            END FUNCTION
            How long is an idea?

            Comment


            • #7
              Originally posted by Mike Doty View Post
              To see Windows shutdown options:
              At a command prompt:
              shutdown /? > me.txt
              me.txt
              Very nice, but....
              > Is there a way to automatically close out all users from a program at a specific time?
              The question was not how to log a user out or Windows or shut down a workstation completely

              Comment


              • #8
                I can't get /l switch to log-out user to work.
                They might appreciate saving the energy.
                You are right, they might not want this on all machines.
                How long is an idea?

                Comment


                • #9
                  As an aside, for those who still insist that you should use FIndWindow() and not a mutex to control multiple application instances, note that there are no Windows in either application

                  Comment


                  • #10
                    Code:
                    FUNCTION PBMAIN () AS LONG
                     OPEN EXE.FULL$ AS #1
                     ? "Others locked out"
                    END FUNCTION
                    How long is an idea?

                    Comment


                    • #11
                      Would it be useful for someone (IE : Admnistrator) to have an app that sends a private message to the main app to tell it to terminate connections and shut down ? Could also be done by TCP or UDP.
                      hutch at movsd dot com
                      The MASM Forum

                      www.masm32.com

                      Comment


                      • #12
                        Originally posted by Mike Doty View Post
                        Code:
                        FUNCTION PBMAIN () AS LONG
                        OPEN EXE.FULL$ AS #1
                        ? "Others locked out"
                        END FUNCTION
                        Again, very nice but...
                        > close out all users from a program...to leave at the end of the day without closing the program

                        i.e the perceived* need was to shut down an application running on some workstation, not preventing a new instance from opening
                        This doesn't solve the OPs problem.

                        (*perceived, because it was not an optimal solution to the actual problem )

                        Comment


                        • #13
                          I just knocked up a second demo to answer the OP's original question. Shut down an application if it runs past a certain time:

                          https://forum.powerbasic.com/forum/u...ime#post802803

                          Comment


                          • #14
                            Originally posted by Mike Doty View Post
                            I can't get /l switch to log-out user to work.
                            They might appreciate saving the energy.
                            You are right, they might not want this on all machines.
                            A widely reported "feature". The Help listing is incorrect.
                            If you include "/t" or "/c" , then "/l" doesn't do anything.
                            The timer switch only work with shutdown, sleep, hibernate (not all versions) and restart.
                            It's been that way since Win 7 at least.

                            One solution would be to use a simple
                            SHELL("shutdown.exe /l")
                            with a Timed Message Box

                            Code:
                            #COMPILE EXE
                            %UNICODE = 1
                            #INCLUDE "WIN32api.inc" 'PBWIn 10 includes
                            GLOBAL gwszMsgBoxTitle AS WSTRINGZ * 256
                            GLOBAL gFlgTimedOut AS LONG
                            
                            FUNCTION PBMAIN () AS LONG
                               LOCAL dwTimerID AS DWORD
                               LOCAL lRes AS LONG
                               gwszMsgBoxTitle = "Logging Out"
                               dwTimerID = SetTimer(0, 0, 3000, CODEPTR(mbox_TimedMsgBoxProc))
                               lRes = MSGBOX("Logout in 3 seconds unless you click 'Cancel' ", %MB_OKCANCEL, gwszMsgBoxTitle)
                                KillTimer 0, dwTimerId
                                IF ISTRUE(gFlgTimedOut) OR (lRes = %IDOK) THEN
                                    lRes = SHELL("shutdown.exe /l")
                               END IF
                            END FUNCTION
                            
                            SUB mbox_TimedMsgBoxProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL idEvent AS DWORD, BYVAL dwTime AS DWORD)
                               gFlgTimedOut = 1
                               SendMessage FindWindow("", gwszMsgBoxTitle), %WM_CLOSE, 0, 0
                            END SUB
                            Last edited by Stuart McLachlan; 17 Dec 2020, 07:38 AM.

                            Comment


                            • #15
                              Just noticed this is the Console Compiler forum so MsgBox won't compile

                              Stuart, thanks for getting /l option of shutdown to work.
                              > If you include "/t" or "/c" , then "/l" doesn't do anything.

                              Note: above line 12, szMsgBoxTitle slipped in, should be the defined global gwszMsgBoxTitle.
                              Also, really like the timed message box (a real keeper.)

                              Code:
                              #COMPILE EXE
                              %UNICODE = 1
                              #INCLUDE "WIN32api.inc" 'PBWIn 10 includes
                              GLOBAL gwszMsgBoxTitle AS WSTRINGZ * 256 'logoutuser.bas
                              GLOBAL gFlgTimedOut AS LONG
                              
                              FUNCTION PBMAIN () AS LONG
                                 LOCAL dwTimerID AS DWORD  
                                 LOCAL lRes AS LONG
                                 LOCAL Seconds AS LONG
                                 Seconds = 10   'Slight addition to Stuart McLachlan's
                                 gwszMsgBoxTitle = "Logging Out"
                                 dwTimerID = SetTimer(0, 0, Seconds * 1000, CODEPTR(mbox_TimedMsgBoxProc))
                                 lRes = MSGBOX("Logout in" + STR$(Seconds) + " seconds unless you click 'Cancel' ", %MB_OKCANCEL, gwszMsgBoxTitle)
                                  KillTimer 0, dwTimerId
                                  IF ISTRUE(gFlgTimedOut) OR (lRes = %IDOK) THEN
                                      lRes = SHELL("shutdown.exe /l")
                                 END IF
                              END FUNCTION
                              
                              SUB mbox_TimedMsgBoxProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL idEvent AS DWORD, BYVAL dwTime AS DWORD)
                                 gFlgTimedOut = 1
                                 SendMessage FindWindow("", gwszMsgBoxTitle), %WM_CLOSE, 0, 0
                              END SUB
                              How long is an idea?

                              Comment


                              • #16
                                Originally posted by Mike Doty View Post
                                Just noticed this is the Console Compiler forum so MsgBox won't compile

                                Stuart, thanks for getting /l option of shutdown to work.
                                > If you include "/t" or "/c" , then "/l" doesn't do anything.

                                Note: above line 12, szMsgBoxTitle slipped in, should be the defined global gwszMsgBoxTitle.
                                Also, really like the timed message box (a real keeper.)
                                ]
                                Doh! Forgot it was in Console Compiler by the time I'd got to that post.

                                Thanks, I've edited the earlier post to correct the "szMsgBoxTitle"

                                I think the timed message box came from somewhere here in the forums many years ago - the inc file for it in my code library folder is dated 2009

                                There's also a similar one from 2001 here: https://forum.powerbasic.com/forum/u...-box#post23592

                                Comment


                                • #17
                                  Nothing stops you from using MessageBox() in PBCC.
                                  hutch at movsd dot com
                                  The MASM Forum

                                  www.masm32.com

                                  Comment


                                  • #18
                                    You can set a waitable timer when the program starts, reading the "quitting time" from a cinfiguration file or database if you like.

                                    Waitable Timer Object Demo June 2005

                                    That demo includes options for either 'elapsed time' or 'time certain' and an IDE to play with to see it work.

                                    Also shows you how to 'cancel' the timer... in your case when the user does not forget to end the program.


                                    MCM
                                    Michael Mattias
                                    Tal Systems (retired)
                                    Port Washington WI USA
                                    [email protected]
                                    http://www.talsystems.com

                                    Comment


                                    • #19
                                      Originally posted by Michael Mattias View Post
                                      You can set a waitable timer when the program starts, reading the "quitting time" from a cinfiguration file or database if you like.

                                      Waitable Timer Object Demo June 2005

                                      That demo includes options for either 'elapsed time' or 'time certain' and an IDE to play with to see it work.

                                      Also shows you how to 'cancel' the timer... in your case when the user does not forget to end the program.


                                      MCM
                                      "Author: Michael Mattias Racine WI USA June 4 2995"

                                      Well at least it's not ancient code for change

                                      All together now:
                                      Let's do the Time Warp, again!

                                      Comment


                                      • #20
                                        Well at least it's not ancient code for change
                                        I would like a nickel for every person who has been helped by my "ancient" code. I'll bet that is many nickels.

                                        Besides, using a synchronization object beats the snot out of using a regular timer and checking every ten seconds or so....talk about an ancient technique!
                                        Michael Mattias
                                        Tal Systems (retired)
                                        Port Washington WI USA
                                        [email protected]
                                        http://www.talsystems.com

                                        Comment

                                        Working...
                                        X