I have a a wait form FormWait (long running task notification), that has ShowMessage(string message) function.
Often happens in code:
public RootCall()
{
FormWait.ShowMessage("Begin long task 1...");
ChildCall();
FormWait.CloseForm();
}
public ChildCall()
{
FormWait.ShowMessage("Begin long task 2...");
// some code here
FormWait.CloseForm();
}
FormWait on root shows the message to the user, but before closing it on root level, there is another ShowMessage of child and CloseForm of child.
I have a couple of solutions to resolve this:
-
Like in code provided the methods are static and operate on one static
System.Windows.Forms.Forminstance. On everyShowMessagethere is a static variable that increments and on everyCloseFormit decrements. So by looking on that variable I can understand if I really need to close the form (if I’m or not on root level), or its just a nestedCloseFormcall. And on everyShowMessagenew string just updated on already visible form. -
For every new
ShowMessagecall create new instance of the form, but this is really wired to see. So almost sure I will not pick this solution.
Any ideas, how can I manage WaitForm (form that signals to user about long running tasks) in case of nested calls, by making also the developer life easier.?
The Stack<> class is the natural fit for this:
Don’t forget to put the CloseForm() call in a finally block so this is all exception safe.