0
votes

I have an Autohotkey script that displays a msgbox and is always on top (very important to have this). I would like the msgbox to be closed ONLY under these 2 conditions:

  1. I manually close it myself.
  2. When a specific window is closed, then close the msgbox.

Here's the main part of the code that I currently have:

    msgbox,262144,TimesheetBlah,% list "`n" "`n" list2

    clipboard := listCopy

    WinActivate,Manage: Time Entry
    WinShow, Manage: Time Entry
    WinWait, Manage: Time Entry
    WinWaitClose  ; Wait for the exact window found by WinWait to be closed.
    ControlClick, Button1, TimesheetBlah // This should close the msgbox, but it doesn't

    if WinExist("*TimesheetBlah*"){
        WinClose ; use the window found above // This doesn't close the msgbox either

How can I achieve this?

2

2 Answers

2
votes

This should work

WinShow, Manage: Time Entry
WinWait, Manage: Time Entry
WinActivate,Manage: Time Entry

SetTimer, CloseMsgBox, -1000    ; run only once
msgbox,262144,TimesheetBlah,% list "`n" "`n" list2

CloseMsgBox:
    WinWaitClose, Manage: Time Entry
    WinClose, TimesheetBlah ahk_class #32770
Return

because a timer's thread can interrupt other threads (in this case the msgbox thread).

0
votes

You are pausing that thread by displaying a MsgBox. Code execution will only continue once you close the MsgBox.
You're either going to want to make your own custom gui that would be like a message box, or run the message box in a separate thread.

Running in a separate thread seems to be showcased already in the other answer, so here's creating your own GUI:

Gui, +AlwaysOnTop +ToolWindow ;see docs for description of options
Gui, Add, Text, y20, Hi!
Gui, Add, Button, x96 y63 w75 h23 gGuiClose, OK ;go-label 
Gui, Show, Center w181 h91, NiceTitleForOurMsgBox

WinActivate, Untitled - Notepad
WinShow ;can use the last found window, though this command is doing nothing for us(?)
WinWait, Untitled - Notepad
WinWaitClose, Untitled - Notepad
WinClose, NiceTitleForOurMsgBox
;Sure, we could also control click, but lets not.
;ControlClick, Button1, NiceTitleForOurMsgBox
Return

;runs automatically when the gui is closed,
;or also runs from the OK button (via g-label)
GuiClose()
{
    ExitApp
}

I made the gui somewhat resemble a message box. Of course it could be customized further to look exactly as a message box, but I'd assume that's not the point.
I assume you're just trying to constantly display some information on the screen.
And tbh, I wouldn't say message box is really intended for that.

So might be worth looking into guis, would open so many other possibilities as well.
For example, maybe you'd want to make your gui be an actual semi-transparent click-through overlay, instead of some window with an OK button.