Question

Running this code:

Window_Abfrage.IsEnabled = false;
Alert(Cloud.UserExists(new Cloud.User(TextBox_Cloud_User_UserName.Text)).ToString()); // unimportant for the problem; just a function causing a few seconds of time
Window_Abfrage.IsEnabled = true; 

doesnt work how I want. The idea is, that the window is disabled, while the function Cloud.UserExists is running, but in fact the window is enabled all the time and just at the end it switches to disabled, only a short time before the alert is shown. Do you know why?

Best Regards!

Was it helpful?

Solution

This is happening because UI freezes (due to long running task you are invoking) before it can render as disabled. In .NET 2.0/3.5 The best way to achieve desired behavior is to use BackgroundWorker.

  1. In its DoWork event (which is executed on separate thread) you should run your long running task.
  2. In its RunWorkerCompleted event (executed on UI thread) you should enable Window.
  3. In button click event, run the BackgroundWorker this way:

    Window_Abfrage.IsEnabled = false;
    backgroundWorker.RunWorkerAsync();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top