Dante1986
Dante1986

Reputation: 59979

C# freeze process while using Show instead of ShowDialog

I am writing an addin for an specific program (Autodesk Revit). Within this addin, i am showing a WinForm to the user. With a button on the winform, the user will be temporary sent back to the main application (Autodesk Revit) to make a selection, and once finished it will be sent back to the WinForm.

When initializing the Winform, i can either use the Form.Show() or use Form.ShowDialog()

If i would be using the Form.Show() The form will proper display and a button on the form can be used to let the user make a selection in the main application and send him back to the form when he is done with his selection using this.hide()before the selection and this.Show() after to make the form temporarily hidden during selection process.

If i would be using the Form.ShowDialog() The form will proper display and a button on the form can be used, but because the ShowDialog freezes the main application while waiting for the dialogresults, the user wont be able to make a selection.

So thats why i would want to go with the Form.Show() instead of Form.ShowDialog().

But here is the issue i am facing: If i use the Form.Show(), the form can be moved to the background or another monitor, and because the main application is not in a frozen state, the user can just continue working and completely forget the form is still open.

So i am wondering if it is possible to somehow idle/freeze the main application as if i was using the ShowDialog while in reality using the Show ?

Upvotes: 0

Views: 1204

Answers (2)

Johan Donne
Johan Donne

Reputation: 3285

I think you need to adjust your logic a bit:

  • use ShowDialog, but close the form to return to the original application
  • devise a method of saving the forms state (e.g. serialize to a MemoryStream) before closing.
  • recreate the form and restore its state before showing it modal again when you want the user to be sent to the form again.

Upvotes: 2

Afnan Makhdoom
Afnan Makhdoom

Reputation: 654

Using another thread won't hang your main form when using ShowDialogue() Use the code below:

        //Initially
        this.Enabled = false;
        Form form = new YourForm();
        form.TopMost = true;
        form.Focus();
        System.Threading.Thread thread = new System.Threading.Thread(() => form.ShowDialog());
        thread.Priority = System.Threading.ThreadPriority.Highest; //<- just an example, set it according to your need
        thread.Start();



        //If you want to close it from the main form (you can do it from the opened form itself though)
        form.Invoke((MethodInvoker)delegate
        {
            form.Close();
        });
        thread.Abort();
        this.TopMost = true;
        this.Enabled = true;
        this.Focus();
        this.TopMost = false;

Upvotes: 0

Related Questions