简体   繁体   中英

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?

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 (eg, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM