Understanding the true DispatcherPriority enumeration behavior provided in WPF

There is documentation with definitions for what each lists . But how can I demo or see this in practice? And how can I find out when to use this priority?

Here is some code that I created to see how priorty affects the order, and it gives me confirmation of the correct ordering (the first iteration of the loop will add the SystemIdle queue to the dispatcher queue), but this is still added to the last line

private void btn_Click(object sender, RoutedEventArgs e)
    {
        StringBuilder result = new StringBuilder();
        new Thread(() =>
            {

                var vals = Enum.GetValues(typeof(DispatcherPriority)).Cast<DispatcherPriority>().Where(y => y >= 0).ToList();
                vals.Reverse();
                vals.ForEach(x =>
                    { 
                        Dispatcher.BeginInvoke(new Action(() =>
                        {
                            result.AppendLine(string.Format("Priority: {0} Enum:{1}", ((int)x), x.ToString()));
                        }), x);
                    });


            }).Start();

        ShowResultAsync(result, 2000);
    }

    private async void ShowResultAsync(StringBuilder s, int delay)
    {
        await Task.Delay(delay);
        MessageBox.Show(s.ToString());
    }

enter image description here

and the output order remains the same even if the list is canceled (this line is added immediately after the assignment vals):

vals.Reverse();

So again, is there anything more that I can use when deciding which priority I should assign?

+4
1

Prism Framework DefaultDispatcher, Dispatcher, Normal. - .

/// <summary>
/// Wraps the Application Dispatcher.
/// </summary>
public class DefaultDispatcher : IDispatcherFacade
{
    /// <summary>
    /// Forwards the BeginInvoke to the current application <see cref="Dispatcher"/>.
    /// </summary>
    /// <param name="method">Method to be invoked.</param>
    /// <param name="arg">Arguments to pass to the invoked method.</param>
    public void BeginInvoke(Delegate method, object arg)
    {
        if (Application.Current != null)
        {
            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, method, arg);
        }
    }
}

- , .

- "" , Background.

, NuGet, Send, Normal, Background ApplicationIdle , WPF DispatcherPriority .

0

Source: https://habr.com/ru/post/1615829/


All Articles