Order InvokeAsync

I have a different scenario than this question , so another question.

Take the following example:

void PerformanceCriticalMethod()
{
    Dispatcher.Invoke(() => Log.Add("1"));
    ... some job
    Dispatcher.Invoke(() => Log.Add("2"));
}

Invokecreates performance issues. In an attempt to fix this, I used InvokeAsyncinstead Invokeand it seems to work, but it is important what 2will be output after 1 .

Can I rely on the assumption that if two InvokeAsyncare called from the same thread, they will execute in the same order? My winapi nut tells me this, but I want to be sure.

And another related question is the same as the related one: if the same method is called through an InvokeAsyncaction from different threads, how good are the chances of executing it first for the thread that it calls InvokeAsyncbefore?

P.S.: , " InvokeAsync", "", , 100% , ( ) .

+2
4

Dispatcher.InvokeAsync() DispatcherOperation. Task:

T: System.Threading.Task, .

, Task. , .

Dispatcher.InvokeAsync(() => Log.Add("1")).Task.ContinueWith(() => {
    Dispatcher.InvokeAsync(() => Log.Add("2"));
});

Itzchakov, , , await InvokeAsync , DispatcherOperation awaitable ( GetAwaiter ).

, InvokeAsync , ?

. ( , ()).

+1

Dispatcher.InvokeAsync DispatcherOperation, ( GetAwaiter). , await :

async Task PerformanceCriticalMethodAsync()
{
    await Dispatcher.InvokeAsync(() => Log.Add("1"));
    ... some job
    await Dispatcher.InvokeAsync(() => Log.Add("2"));
}
0

:

, InvokeAsync, .

async, " ", InvokeAsync / ( , @olegk).

0

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


All Articles