1

I query the database on a separate thread and generate a MessageBox for the user. I want the user to stop what he's doing and respond to the MessageBox.

With my code below, the message box doesnt even pop up over the current window. The user needs to click on the taskbar to see it. How can I get it to interrupt the main thread ?

Dim Button = MessageBoxButton.YesNo
Dim icon = MessageBoxImage.Information
Dim result = MessageBox.Show("Hi", "Title", Button, icon)

Select Case result
  Case MessageBoxResult.Yes
    queryInProgress = 0
    cashTransfer.TransactionApproved = 1
  Case MessageBoxResult.No
     queryInProgress = 0
     cashTransfer.TransactionApproved = 0
End Select
  • More details on the how your thread is created would be useful. For example, if you initiated it with `Thread.Start()`, you could pass an object to the delegate: `Thread.Start([Object])`. This object might contain a SynchronizatonContext and the reference to a Callback in the main thread that can be used in the working method of the Thread to synchronize the UI in the main thread, so that a dialg box can be presented to the user. Using the SynchronizatonContext `.Send()` method, the Thread continues or aborts only after the Dialog Box has been closed. – Jimi Jan 11 '18 at 05:14

1 Answers1

0

You need to invoke it on the ui-thread. If your'e using winforms, the Application class has a collection of currently open forms for you. Usually the first should be the ui-created form you create with Application.Run().

Application.OpenForms[0].Invoke(new Action(() =>
{
    MessageBox.Show("Test");
}));

Hope it works for you.

Otterprinz
  • 459
  • 3
  • 10
  • Does WPF also have this option – user9199295 Jan 10 '18 at 15:37
  • @user9199295 take a look at the answer listed here: https://stackoverflow.com/questions/2877094/wpf-version-of-application-openforms – Sasha Jan 10 '18 at 15:46
  • 1
    WPF does not have a 1:1 replacement for System.Windows.Forms.Application, but System.Windows.Application, the base class for WPF apps has a properties named MainWindow and Dispatcher. You can try to Invoke on the Dispatcher. – Otterprinz Jan 10 '18 at 15:49