tofutim
tofutim

Reputation: 23374

Does TaskScheduler.FromCurrentSynchronizationContext() mean the context of the class?

I used to use Dispatcher or setting the SynchronizationSetting but since switching to PCL (Profile 158), I'm left with FromCurrentSynchronization, Default or Current. I think that FromCurrentSynchronization means the syncContext of the class. Is that true? How do I check it?

Currently the class uses

    private async Task asyncInvoke(Action action)
    {
        await Task.Factory.StartNew(action,
            CancellationToken.None,
            TaskCreationOptions.None,
            TaskScheduler.FromCurrentSynchronizationContext());
    }

If I stick this in a utility class, will it end up picking up the syncContext of the utility class?

Upvotes: 0

Views: 359

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 456497

FromCurrentSynchronizationContext is what you're looking for. It is not related to a "class" at all.

As Hans stated, one way of thinking of it is that it's a property of the current thread.

In your case, a Dispatcher-based UI will provide a SynchronizationContext for the UI thread that is tied to the dispatcher. An easy way to verify this is to check out SynchronizationContext.Current.GetType().Name.

However, I recommend that you consider a more radical approach. You're currently trying to update the UI from background thread logic. Try to restructure your code so that your background thread logic is not aware of the UI at all (e.g., see the Progress Reporting section on this page on MSDN). If you can do this, you'll find that your code is more testable and has better separation of concerns.

Upvotes: 1

Related Questions