开发者

c# Concurrency and order - Dispatcher.BeginInvoke

开发者 https://www.devze.com 2023-03-15 16:11 出处:网络
I have a smal开发者_如何学Pythonl problem: I got an object Log. This object fires an event when it\'s write function is called. The write function is not necessary called on the current applications d

I have a smal开发者_如何学Pythonl problem: I got an object Log. This object fires an event when it's write function is called. The write function is not necessary called on the current applications dispatcher. So assume the following code:

protected void OnLoggingEnqueuedMessage(object sender, EnqueuedMessageEventArgs e)
{
    if (Application.Current.Dispatcher.CheckAccess())
        AddLogEntry(e.LogEntry);
    else
        Application.Current.Dispatcher.BeginInvoke(new Action(() => { AddLogEntry(e.LogEntry); }), null);
}

If Application.Current.Dispatcher.CheckAccess() returns false the order of items added can be messed up. I understand that operations enqueued with BeginInvoke are not necessary executed in the same order as BeginInvoke is called but how can i add the items in correct order?


You said:

I understand that operations enqueued with BeginInvoke are not necessary executed in the same order as BeginInvoke

however the documentation states

If multiple BeginInvoke calls are made at the same DispatcherPriority, they will be executed in the order the calls were made.

and as you are not specifying a DispatcherPriority your calls will have the same priority.

0

精彩评论

暂无评论...
验证码 换一张
取 消