0

I would like to achieve this:

  1. Create a new window, which is just a spinning loader and some static text, and display it.
  2. Do some computation on the UI thread (it has to be done there)
  3. Close the loader window

Currently, when I call .Show() on the loader window, it doesnt load at all, it just hangs out as a blank window (the UI thread is blocked, which I dont want)

What can I do to enable the loader window to display its contents at the same time my computation is done?

private void CopyToClipboard(object sender, RoutedEventArgs e)
    {
        var selected = mDataGrid.SelectedItem;
        var selectedIndex = mDataGrid.SelectedIndex;
        var progressWindow = mProgressDialog.Create(this,"Copying data to clipboard");
        progressWindow.Show();
        try
        {

            mDataGrid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
            mDataGrid.SelectionMode = DataGridSelectionMode.Extended;
            mDataGrid.SelectAllCells();  // SLOW !!!!
            ApplicationCommands.Copy.Execute(null, mDataGrid); //SLOW !!!!
        }
        catch (Exception ex)
        {
            mLog.Error("Copying to clipboard", ex);
            MessageBox.Show("Error while copying to clipboard");
        }
        finally
        {
            mDataGrid.SelectionMode = DataGridSelectionMode.Single;
            mDataGrid.UnselectAllCells();
            this.Select(selectedIndex, selected);
            progressWindow.Close();
        }
    }
H.B.
  • 166,899
  • 29
  • 327
  • 400
Tomas Grosup
  • 6,396
  • 3
  • 30
  • 44
  • 4
    Just wondering why your computation has to be done on the UI thread? – Surfbutler Jul 16 '12 at 12:27
  • 1
    Maybe his calculation manipulates some objects databound to UI elements, and if they "change" on another thread, everything blows up. – mathieu Jul 16 '12 at 12:31
  • You can actually see in in the code, it is selecting all cells of a datagrid and copying them to the clipboard = manipulating UI controls – Tomas Grosup Jul 16 '12 at 16:38

2 Answers2

1

As you said, your calculation is done on the UI thread, that's why your window is blank, it is not "repainted" due to the UI thread being busy.

So unless you do your long running calculation on another thread, you're quite stuck.

mathieu
  • 30,974
  • 4
  • 64
  • 90
  • I know the thread is busy, isnt there for example a way of executing the "loader window" on a totally new UI thread? As you can see in the code, the computation manipulates a datagrid, so it has to be in the UI thread. (or can I for example copy the whole datagrid to a new UI thread/process/... and execute it there?) – Tomas Grosup Jul 16 '12 at 16:42
0

Only thing I was able to achieve is this:

progressWindow.Show();
this.DoEvents();


public static void DoEvents(this Window _)
    {
        Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                              new Action(delegate { }));
    }

The "loader window" gets at least displayed, so the user knows something is happening. It usually lasts under a second, so this solution is acceptable to me.

Tomas Grosup
  • 6,396
  • 3
  • 30
  • 44