I have a very annoying problem and I'm trying to find the simplest solution possible, but I seem to keep complicating it.
My program displays a custom dialog box using the ShowDialog function (I create this form using a New constructor), but while my program waits for input in the dialogbox, my program continues to run other code, (I'm assuming initiated from other messages, but I still can't find good information on this, so as an aside to this topic does anyone have a reference about this), and arrives at this same point where the dialog box is to be displayed and displays it. So I end up getting a flood of dialog boxes.
My goal is prevent this excessive amount of dialog boxes. If the program arrives at this dialog initiation point I would like it to wait/pause until the first dialog box is finished.
I thought about threaded solutions, background workers, all kinds of ideas but they just seem to complicate things and I haven't actually fixed it.
Does anyone have an idea how I could prevent this from occurring?
Your help is greatly appreciate. Thanks
It seems to me that you have some very ugly bug in your code! I'd focus on fixing this bug first. The easy way to do this is to set a breakpoint on the line that opens up the dialog. When the line is hit the second time (while the first dialog box is still open), view the stacktrace at that point and check all active threads (View Threads and View Stack windows) for the cause of the issue.
In addition, change your code to open a modal dialog window instead, where the parent window must be the owner. A modal dialog window will stop execution and prevent the parent window from processing user input (which could've lead to this behavior).
From your comments I gather that you have several threads that you cannot control and that you want one thread at a time to access the messagebox code. When working with threads, there are several synchronisation primitives available. Expanding on each of them requires a book (you may try Concurrent Programming on Windows, one that's thorough while the book has some flaws in its structure), knowing the right one for you requires knowledge of your code.
That said, you may wish to do something like the following, using a Mutex which at least prevents other threads to access the code (meaning: it will put them in a suspend state until the Mutex is released). Add a static boolean flag variable (or add a checkbox on the popupped form "show box only once") if you want to prevent the popup box to show more than once. Mutex plus flag solves two problems in one: only one thread will ever run that code, and the code will only run once, ever.
// as static class variable, create a mutex
private static Mutex dialogMutex = new Mutex();
// a static flag preventing the dialog box to show more than once
// (you may wish to resolve this differently, depending on req's)
private static boolean dialogIsShownOnce = false;
public static void ShowDialogBox()
{
// Wait until it is safe to enter, this makes the current thread
// the exclusive user of this code and other threads may only enter
// after the current thread finishes.
dialogMutex.WaitOne();
// depending on your requirements, you may not want this
// must come _after_ WaitOne to prevent entering before another
// thread that entered hasn't yet changed this variable
if(dialogIsShownOnce)
return;
// show your dialog box as a modal box
// if you are unsure: add a breakpoint just after the ShowDialog
// it should only be hit _after_ you dismiss the dialog box
yourForm.ShowDialog();
// set the flag, or the counter, or whatever you wish:
dialogIsShownOnce = true;
// Release the Mutex, this will remove the "roadblock" and allow
// other threads to enter this piece of code
dialogMutex.ReleaseMutex();
}
The code above is not tested and should be considered a hint on how to tackle this issue. There are so many ways to solve this, but I think the above method may just be what you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With