Have any of you come up with a standardized way to display a Message Dialog from within a thread?
In the event of an error in the threaded task that can be resolved by user action, I need to pause the thread execution, display the MessageDialog, then act on the input from the dialog. I’ve tied using a Timer to fire the dialog, but this has turned out to be hit or miss as to whether the MessageDialog is displayed before the thread moves forward to the following instructions.
A couple of years ago I wrote a tutorial for xDev on how to create a messaging system, this is how I would do it. Your thread sends a message asking for a message box to be displayed and the contents. Another part of your app then receives that message and displays the message dialog.
If you wanted to make it interactive, the thread would pause after sending the message, the other part of the GUI would then set a property on the thread and ask it to resume.
The messaging system not only allows for values to be transported, but also the sender so the GUI would know exactly which thread to resume.
[code]dim currentThread as Thread = App.CurrentThread
if currentThread = nil then
// the main thread is active - we can simply call MsgBox here
HandleAction(nil)
return
end if
'create the timer
me.ShowDialog = new DelegatingTimer
me.ShowDialog.RunMode = Timer.RunModes.Single
me.ShowDialog.Period = 100
me.ShowDialog.Action = AddressOf me.HandleAction
theThread = currentThread
theThread.Pause[/code]
Replace the DelegatingTimer with a normal timer and use CallLater instead. The MessageDialog is displayed in the HandleAction method.
[quote=476513:@Beatrix Willius]theThread = currentThread
theThread.Pause
[/quote]
I had the rest, but the obvious escaped me - theThread.Pause … D’OH! In my case, that’s what I needed for this project.
However, as I look to new projects that may use API 2, I’ll look into Scott’s recommendation.