Chris M
Chris M

Reputation: 96

WPF context menu won't fully close before data-intensive operation

I have a WPF application where a data-intensive operation is called by a context menu selection.

I'm using Dispatcher to run the data-intensive operation in the background, but the context menu does not fully close before it begins - it starts to close (i.e. fade) but doesn't disappear completely before the operation begins. The result is a sloppy looking half-faded context menu sitting open on the screen while my "executing" message displays and the data operation completes.

I understand that there is a separate "rendering" thread that runs in the background of a WPF application, and it seems to me like the Close operation of the context menu is completing but the rendering event of fading the context menu away is not finishing before the data-intensive operation begins.

Here is my code:

private void mnuAddProblem_Click(object sender, RoutedEventArgs e)
{
    CareGuide selectedCareGuide = (CareGuide)grdCareGuides.SelectedItem;
    List<Problem> selectedProblems = new List<Problem>();

    for (int i = 0; i < grdSuggestedProblems.SelectedItems.Count; i++)
    {
        selectedProblems.Add((Problem)grdSuggestedProblems.SelectedItems[i]);
    }

    LoadingWindow loader = new LoadingWindow();
    loader.Owner = this;
    loader.WindowStartupLocation = WindowStartupLocation.CenterOwner;

    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += delegate
    {
        Dispatcher.BeginInvoke(new Action(delegate { loader.ShowDialog(); }));
        Dispatcher.Invoke((Action)(() => { dataWorker.AddSuggestedProblems(selectedProblems, selectedCareGuide); }));
        Dispatcher.BeginInvoke(new Action(delegate() { loader.Close(); }));
    };
    worker.RunWorkerAsync();                   
}

Upvotes: 1

Views: 237

Answers (1)

Chris M
Chris M

Reputation: 96

I was able to fix this by disabling the "fading" animation that occurs when opening and closing the context menu by overriding the system parameter that controls popup animation:

<PopupAnimation x:Key="{x:Static SystemParameters.MenuPopupAnimationKey}">None</PopupAnimation>

Upvotes: 1

Related Questions