Announcement

Collapse
No announcement yet.

Auto closing a MsgBox

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

  • Michael Mattias
    replied
    DUH, where is my head today....

    WFSO is going to return in 'millseconds' or less... whichever occurs first:
    A. User hits a key (PBCC) or clicks OK on MSGBOX (PB/Win) (ends function, signals the thread object resulting in WFSO returning WAIT_OBJECT_0)
    B. 'Millseconds' elapse. (WFSO returns WAIT_TIMEOUT)

    ... which is what you wanted.

    However, the function TimedMessageBoxThread() is going to execute until "A" occurs or the process ends.

    MCM
    Last edited by Michael Mattias; 22 Jul 2009, 03:25 PM.

    Leave a comment:


  • Mike Doty
    replied
    WaitForSingleObject is required.
    Pressing/clicking would always wait with a SLEEP statement.
    This code could be improved to return if user performed an action or time was reached.

    Code:
    #DIM ALL    'MessageBoxWait.bas
    #COMPILE EXE
    #INCLUDE "win32api.inc"
    GLOBAL gsMessageBox AS STRING
    FUNCTION PBMAIN AS LONG
       MessageBoxWait "How now brown cow", 2000
       ? "Done"
    END FUNCTION
    SUB MessageBoxWait (s AS STRING,milliseconds AS DWORD)
       LOCAL hThread AS DWORD
       IF LEN(COMMAND$) THEN                'override passed value
        milliseconds = ABS(VAL(COMMAND$))  'use value in COMMAND$
      END IF
      gsMessageBox = s
      THREAD CREATE TimedMessageBoxThread(milliseconds) TO hThread
      WaitForSingleObject hThread, milliseconds
      REM SLEEP milliseconds  'wrong, would always wait
      THREAD CLOSE hThread TO hThread
    END SUB
    THREAD FUNCTION TimedMessageBoxThread(BYVAL milliseconds AS DWORD) AS LONG
     #IF %DEF(%PB_CC32)
      ? gsMessageBox
      WAITKEY$
    #ELSE
      ? gsMessageBox
    #ENDIF
    END FUNCTION
    Last edited by Mike Doty; 22 Jul 2009, 11:49 AM.

    Leave a comment:


  • Michael Mattias
    replied
    >WaitForSingleObject hThread, milliseconds

    In the demo shown this may as well be ....
    Code:
    SLEEP milliseconds
    .. because the only reason this 'works' is that you reach the end of PBMAIN when WFSO returns at which point the compiler calls ExitProcess which terminates all threads of execution.

    Which of course means, this function is not usable EXCEPT when it's the last thing the program does.

    If you waited %INFINITE it would wait on the user to Hit a key (PBCC) or click OK (PBWIN).

    Leave a comment:


  • Michael Mattias
    replied
    If sending that message to a console screen instead of a GUI-type message box would be OK...
    Wait for key, click or clock for PB/WIN and PB/CC

    (You'll never guess from the title what it does!)

    Leave a comment:


  • Mike Doty
    replied
    Here is one that also works on a command line which is useful within batch files.
    Using a timer will not always work as expected if you use a statement like TCP OPEN which is in a wait state.
    Running in another thread solves that problem.
    Code:
    #DIM ALL
    #COMPILE EXE  'Sleeper.bas  [URL]http://www.powerbasic.com/support/pbforums/showthread.php?t=40472[/URL]
    #INCLUDE "win32api.inc"
    GLOBAL gsMessageBox AS STRING
    FUNCTION PBMAIN AS LONG
       gsMessageBox = "Press Enter, click OK or wait 2000 milliseconds"
       Sleeper 2000  'wait 2000 milliseconds
     END FUNCTION
    FUNCTION Sleeper (milliseconds AS DWORD) AS LONG
       LOCAL hThread AS DWORD
       IF LEN(COMMAND$) THEN                'override passed value
        milliseconds = ABS(VAL(COMMAND$))  'use value in COMMAND$
      END IF
      THREAD CREATE TimedMessageBoxThread(milliseconds) TO hThread
      SLEEP 50
      WaitForSingleObject hThread, milliseconds
      THREAD CLOSE hThread TO hThread
     END FUNCTION
    THREAD FUNCTION TimedMessageBoxThread(BYVAL milliseconds AS DWORD) AS LONG
     #IF %DEF(%PB_CC32)
      ? gsMessageBox
      WAITKEY$
    #ELSE
      ? gsMessageBox
    #ENDIF
    END FUNCTION
    Last edited by Mike Doty; 22 Jul 2009, 06:02 AM. Reason: Did not post correctly. Reposted serveral times.

    Leave a comment:


  • Edwin Knoppert
    replied
    I do not agree with the code above, in this case a close may be ok but actually you should 'click' the button.
    Each button has the same id as the output and should be used instead.
    (Imagne a YES/NO box and so on)

    A msgbox has no close button but the source does close it.. the hard way.

    Leave a comment:


  • Mel Bishop
    replied
    I had he exact same question. You can find the answer here:

    http://www.powerbasic.com/support/pb...ad.php?t=12978

    and/or

    http://www.powerbasic.com/support/pb...ad.php?t=22967

    Leave a comment:


  • Martin Francom
    replied
    José

    Thanks. That's the simpliest solution that I have seen. There were other solutions offerred here. But this is by far the simplest.

    I hope you don't mind, but for easy referrence on this forum I have copied your solution here.

    Code:
    ' code by José Roca
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "WIN32API.INC"
    
    GLOBAL szMsgBoxTitle AS ASCIIZ * 256
    
    SUB TimedMsgBoxProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL idEvent AS DWORD, BYVAL dwTime AS DWORD) 
       SendMessage FindWindow("", szMsgBoxTitle), %WM_CLOSE, 0, 0
    END SUB
    
    FUNCTION PBMAIN () AS LONG
    
       LOCAL dwTimerID AS DWORD
       LOCAL lRes AS LONG
       
       szMsgBoxTitle = "Timed Message Box"
       dwTimerID = SetTimer(0, 0, 5000, CODEPTR(TimedMsgBoxProc))
       lRes = MSGBOX("Timed message box test", %MB_OK, szMsgBoxTitle)
       KillTimer 0, dwTimerId
    
    END FUNCTION

    Leave a comment:


  • José Roca
    replied
    See this discussion:
    Timed Messagebox
    http://www.jose.it-berater.org/smffo...p?topic=1639.0

    Leave a comment:


  • Martin Francom
    started a topic Auto closing a MsgBox

    Auto closing a MsgBox

    How do I auto close a MsgBox ?

    I have a simple little program that creates a message box to tell the user that the operation was successful or not.

    Is there a way to get the MsgBox to AUTO-CLOSE after so many seconds?
Working...
X