Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open window (or dialog) to block main window without stopping further instructions

Tags:

c#

dialog

wpf

I have a MainWindow that will with the click of the button do some calculations that might take a few seconds. During that time I would like to show a little dialog (or separate window) saying "Please wait..." in the middle of MainWindow that will fully block it, so the user is not able to do anything until this dialog is closed. I tried creating a window and opening it with window.ShowDialog(); but that will of course not continue the instructions...

Is there any other way that will work?

like image 988
philkark Avatar asked Jan 28 '26 23:01

philkark


1 Answers

The solution is very straightforward - you should set your MainForm as a parent in the Show (not ShowDialog, as you noticed), and then call Enabled = false on your own form:

        Form2 f = new Form2();
        f.Show(this);
        this.Enabled = false;

        //do your stuff here

        f.Hide();
        this.Enabled = true;
        f.Dispose();
        f = null;

The only problem here is that you cannot cancel your process... But if your calculation is in another thread, the other form's controls will trigger their handlers and you can cancel the work in the other thread.

like image 93
zmilojko Avatar answered Jan 30 '26 11:01

zmilojko