0

I have an app whose threads pop up a modal dialog as appropriate.

If a modal dialog is open when a thread is to pop its own, I need the previous modal dialog to close before allowing the thread to pop a new one.

The following links have helped me understand the problem better but not led to a working solution:

Singleton modal dialog

Windows Forms: wait until another form closes

How can I make execution pause until new form is closed?

preventing multiple instance of one form from displaying

How can I make a single instance form (not application)?

A self-managing form would be ideal. So, mutex? Singleton? A proper implementation eludes me.

Thank you all.

Lara
  • 1,068
  • 2
  • 12
  • 22

1 Answers1

0

You should not open any forms / dialogs from any threads but the main thread that runs your main form.

The correct approach would be to send messages from your threads to your main form using Invoke, or even better BeginInvoke - create a public method on your main form, make the main form available to your threads and use Form.BeginInvoke to call your method.

Your method should take care of the modal dialogs as you wish.

Nick
  • 4,215
  • 1
  • 16
  • 21
  • The multithreaded setup cannot be helped. It is an Office add-in. – Lara Jul 21 '20 at 21:24
  • So what? Office Add-ins also have a "main" UI thread. Please explain. – Nick Jul 22 '20 at 06:13
  • I can simulate multiple calls and use, for example, a CancellationTokenSource to terminate a running modal no problem . But when the code is plugged into Word, no joy. Cannot tell why. – Lara Jul 22 '20 at 06:53
  • But I still don't understand why you need multiple threads. Again, all UI-related operations, including accessing Word from your add-in, should be only from the main thread. – Nick Jul 22 '20 at 10:44
  • I do not need multiple threads. Turns out, they are produced by a mouse hook firing multiple times and calling the add-in so. The modals are set to auto-close after 1 sec (via Task.Delay.ContinueWith > Close). So closing a modal before 1 sec requires interrupting the task and this I can only do while testing outside of Word but not within Word. – Lara Jul 22 '20 at 20:51