0

With Winform Form Window (FixedDialog with toolbars) providing options to install and uninstall the application. (Windows exe application)

When a user clicks on the button to install/uninstall, the window can neither be moved nor minimized. i.e. until the event raised for activity is not completed, it's stuck and not able to perform any action on the form window.

Events are added as below which does independent work. in form1.designer.cs

         private void InitializeComponent(string defaultPath)
        {
        //Other steps
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
        this.InstallButton.Click += new System.EventHandler(this.InstallButton_Click);
        this.UnInstallButton.Click += new System.EventHandler(this.UnInstallButton_Click);
        }

Eg. The function InstallButton_Click has multiple steps for installation which copies files and does other work which takes around half a minute. During this time it doesn't allow to move or minimize the window. In form.cs

private void InstallButton_Click(object sender, EventArgs e)
{
//Multiple steps for installation
//takes around 20-30 seconds to complete
}

The issue is similar to what is mentioned here, but don't see an acceptable answer there.

Is there a way to allow the user to minimize or move the window?

Vaibhav More
  • 212
  • 1
  • 14
  • It sounds like you are simply blocking UI. Are you? Show event hanlders. Please refer to [mcve]. – Sinatr Mar 18 '21 at 09:25
  • @Sinatr, considering install click, it has steps to install which involves copying files which are taking around 20-30 seconds. No activity is allowed on the window during this time. Particularly blocking code is not added from my side. InstallButton_Click() { //Installation steps // If you add thread sleep it should be producible } – Vaibhav More Mar 18 '21 at 09:51
  • 2
    Does this answer your question? [WinForm Application UI Hangs during Long-Running Operation](https://stackoverflow.com/questions/1216791/winform-application-ui-hangs-during-long-running-operation) – Sinatr Mar 18 '21 at 10:04
  • The "modern" way to easily prevent UI from being blocked is to use `async` and `await`. [Duplicate](https://stackoverflow.com/q/1216791/1997232) unfortunately doesn't have those, but it shows several approaches at least. – Sinatr Mar 18 '21 at 10:05
  • 1
    Further possible solutions by Thread/BackgroundWorker/async-await: https://stackoverflow.com/a/36079335/5114784 – György Kőszeg Mar 18 '21 at 10:11
  • It has code to update GUI control back during the process so if any asynchronous way is used it throws error: `Cross-thread operation not valid: Control 'InstallButton' accessed from a thread other than the thread it was created on.` – Vaibhav More Mar 18 '21 at 11:04
  • [Here](https://stackoverflow.com/questions/1216791/winform-application-ui-hangs-during-long-running-operation) are multiple approaches, by mixing some sequential and some async activities solved the issue as of now. – Vaibhav More Mar 18 '21 at 11:06

1 Answers1

0

Here multiple approaches are available.

As the code involved some steps updating the GUI controls back, couldn't use the solutions directly.

As almost all solutions are based asynchronous principle, it used to throw an error, Cross-thread operation not valid: Control 'InstallButton' accessed from a thread other than the thread it was created on.

To avoid it segregated steps involving GUI control access and executed them sequentially while as remaining independent code was run asynchronously using Task.Run() method. It has ignorable execution time for GI

//Logic to update control
Task.Run(()=>
{
 //Remaining logic
});
Vaibhav More
  • 212
  • 1
  • 14