Thread Safe MsgBox Example

Need to display a msgbox inside a thread? Not allowed! That UI access from a thread.

However, pop this module into your project and use

MsgBoxThreadSafe “Hello World”

and it’ll work.

Thought I’d share. :slight_smile:

https://dl.dropboxusercontent.com/u/7273799/MsgBoxThreadSafe.xojo_binary_project

Stephen, perhaps just a suggestion, shouldn’t the following code rather be placed in the Action event of the Test button? When it’s placed in the Activate event of Window1, it keeps showing the message in an endless loop.

MsgBoxThreadSafe "Hello" MsgBoxThreadSafe "World"

Ooops! Pasted the wrong spot. Thanks!

All fixed now.

Also, since you use Messages.pop to get the messages from queue, you should add messages using Messages.insert(0, message) or you are creating a LIFO instead of FIFO queue.

Without subclassing, here it’s version using AddHandler which is possibly more compact.
https://www.dropbox.com/s/j19u97dwi42ox15/MsgBoxThreadSafeAlt.xojo_binary_project

You’re right again. That’s what you get for coding with insufficient sleep. Fixed.

Neat AddHandler redesign.

I absolutely encourage everyone to look at the “Tasks” example included in the IDE download. The overall concept is that you put all of your heavy work inside of the Run event and invoke the UpdateUI method from there when something needs to change. This will punt the data passed in over to the main thread where the UpdateUI event is fired.

The Task and TaskEvent (just a helper class to ignore) can be copied into your own projects. We’ve used this in the IDE in order to make searching be more responsive and are going to be using it in a future release for a few more things.

I created a simple message module that uses a message dialog with the variables passed to it from the running process. In the case of a thread, I use a timer to monitor the thread as recommended for RB/RS/Xojo and an error flag in the thread if a message needs to be displayed. If the timer catches the error flag it passes the message variables from the thread to the message module which then opens a message dialog box. It works like a charm.

Where can I find the “Tasks” example? In which section?

A search function in the examples window would be much appreciated!

Thanks

The tasks example is found at Example Projects -> Desktop -> UpdatingUIFromThread.

Just bumping this old thread. I have been using a Thread and a Timer to update the progress bars in a Desktop (Windows) app and thought I’d replace it with the Task class as per Joe’s suggestion, above.

While it works, what I’m finding is that my progress bars aren’t being updated quickly enough. So for most tasks that means the bar jumps around a little (i.e. doesn’t move very smoothly) and the user never sees the progress bar get up to 100% because the task has moved on to the next thing before the UI is updated. And in some cases with lots of database reading/writing, the progress bar is not re-drawn at all.

Has anybody else seen this and have you found a solution?

Perhaps instead of using a timer to update the UI you could use Xojo.Timer.Calllater http://developer.xojo.com/xojo-core-timer$CallLater with a very short delay. This should give you very granular control over UI updates.

Thanks Wayne. Using a timer to update my UI is already working well for me. My issue is that I wanted to re-factor it to use the Task pattern mentioned by Joe earlier in this thread but, when I did that, I found the UI was updated less often to the point that it was problematic and I was wandering whether anybody else had found this using the Task pattern (sample app) and whether there was a solution.

I like the timer simplicity.