Warum funktioniert SynchronizationContext nicht richtig?

Ich habe folgenden Code:

[TestMethod]
public void StartWorkInFirstThread()
{
    if (SynchronizationContext.Current == null)
        SynchronizationContext.SetSynchronizationContext(
            new SynchronizationContext());

    var syncContext = SynchronizationContext.Current;

    Console.WriteLine("Start work in the first thread ({0})", 
        Thread.CurrentThread.ManagedThreadId);

    var action = ((Action) DoSomethingInSecondThread);
    action.BeginInvoke(CallbackInSecondThread, syncContext);

    // Continue its own work
}

private static void DoSomethingInSecondThread()
{
    Console.WriteLine("Do something in the second thread ({0})", 
        Thread.CurrentThread.ManagedThreadId);   
}

private void CallbackInSecondThread(IAsyncResult ar)
{
    Console.WriteLine("Callback in the second thread ({0})", 
        Thread.CurrentThread.ManagedThreadId);
    var syncContext = (SynchronizationContext) ar.AsyncState;
    syncContext.Post(CallbackInFirstThread, null);
}

private void CallbackInFirstThread(object obj)
{
    Console.WriteLine("Callback in the first thread ({0})",
        Thread.CurrentThread.ManagedThreadId);
}

Ich erwarte, dass die letzte Methode im ersten Thread ausgeführt wird, d. H. Im ersten Thread, aus dem SynchronizationContext stammt, weil ich @ aufrufPost() Methode dieses Kontexts. Das heißt etwas wie das

Start work in the first thread (28)
Do something in the second thread (17)
Callback in the second thread (17)
Callback in the first thread (28)

Ist es nicht die Bedeutung von SynchronizationContext? Aber eigentlich habe ich folgende Ausgabe:

Start work in the first thread (28)
Do something in the second thread (17)
Callback in the second thread (17)
Callback in the first thread (7)

Worin besteht das Problem? Geht etwas mit SynchronizationContext schief oder habe ich ein Missverständnis?

Aktualisieren Ich bezeichne diese Methode als Komponententest mit dem Resharper-Testläufer.

Antworten auf die Frage(6)

Ihre Antwort auf die Frage