Not quite sure what's going on here -
This bit of code is causing issues as it is first called from the Main thread (verified in Task view in VS) and scheduling the tasks, however when setting a breakpoint in UpdateSearchCache
we're now in the worker thread - no longer main!
Subsequent pieces of UI code being called from there fail as they're executed on the worker thread.
Isn't that the whole point of specifying the scheduler? What am I missing?
This code is called when starting our app. It's called from the Bootstrapper of our PRISM app and running on the MainThread.
The SynchronizationContext.Current
is NOT null when the Task is started.
var currentScheduler = TaskScheduler.FromCurrentSynchronizationContext();
var ctx = SynchronizationContext.Current;
if (ctx == null)
throw new NullReferenceException();
Task.Factory
.StartNew(
() =>
SearchHelper.CacheSearchResults(_service.GetData())
.ContinueWith(result => UpdateCache(result.Result), currentScheduler);