Mulitple msgboxes triggered within timer

I have a client app that talks to a device using a timer to look at some flags and responses via serial. If things go wrong on device, the serial thread sets a message which triggers a a msgbox in timer action. On Mac this works fine - single message box pops up and things go on their way once you click ‘OK’. On Win the timer seems to keep going, so I get msgbox popping up on top of msgbox. If I dismiss them all, then things are fine, but obviously not how I want things to work.

So in thread

if something_happened then
TheMsg = “Something Happened”
else
TheMsg = “”
end

and in timer action:

if TheMsg <> “” then
Msgbox(TheMsg)
end

Have toyed with turning the timer mode off and back on based on a few threads I read. Doesn’t help.

This is Win7, things are worse on Win10.

For cleanliness here I would pass the message off to a method so that the timer can keep checking the status of the thread.

Copy the message to a new variable, clear TheMsg, and then do your MsgBox.
With TheMsg cleared you shouldn’t get a million messages even if the timer keeps running.

For example:

if sErrorMessage <> "" then
  dim sLocalMessage as String = sErrorMessage
  sErrorMessage = ""

  MsgBox("Device Error" + EndOfLine + EndOfLine + sLocalMessage)
end

Keep in mind that on Mac the standard message box format is a title and a message, you may want to add this to your MsgBox for consistency. If you’re using MsgBox you can do it with two EOLs, or you can create a custom MessageDialog.

MsgBox("Device Error" + EndOfLine + EndOfLine + TheMsg)

Edit: Code tags, and example.

I would assume from your explanation that a call to MsgBox on the mac will only allow one MsgBox to be open per application yet on Windows it is not. (edit: just tested, this is the case)

As long as TheMsg is not being set every tick of the timer, this should only pop up one message, as Tim mentions, a simple hack would be:

if TheMsg <> "" then dim tmp as string = TheMsg TheMsg = "" Msgbox(tmp) end

If it does, you could simply set a property on the window before you call MsgBox

if TheMsg <> "" And Not MsgOpen then MsgOpen = True Msgbox(TheMsg) MsgOpen = False TheMsg = "" ' (see next post) end

Also, I wouldn’t do:

if something_happened then TheMsg = "Something Happened" else TheMsg = "" end

If you reset TheMsg to “” before you get to call your MsgBox display routine (for whatever reason, race condition, long code path, general hiccup etc.) then you have missed a message.

A simple solution would be:

if something_happened then TheMsg = TheMsg + "Something Happened" + EndOfLine end

So you could have multiple messages for the MsgBox to show, then set MsgBox to “” when you displayed it (see previous posts)

Thanks Tim/Julian - using local var and then clearing the global before MsgBox did the trick. I had tried clearing the global right after MsgBox but didn’t work. Possibly it was taking long enough to create MsgBox on Win the timer would go off again - though it was 1 second.

Code execution would halt on the MsgBox so the variable wouldn’t be cleared until after the MsgBox was closed, which was too late :slight_smile: