I have some code that uses Task.Factory.StartNew() to start a background task when the user clicks a button. Upon completion, the background task then calls Task.Factory.StartNew() but specifies the UI thread context in order to update the UI with the results.
This work fine. However now I have a UI option that will allow the task to run repeatedly until the user cancels it. So now in the second Task.Factory.StartNew() that runs the task on the UI thread, I just call the first function that starts the background task again. However what I'm finding is that the second time around, the first Task.Factory.StartNew() runs the task on the UI thread even though I did not pass in a thread context to it. I'm not sure it matters, but I'm passing in TaskCreationOptions.LongRunning as well for both calls.
To put it another way... In both cases the first Task.Factory.StartNew() was called from the UI thread. The 1st time, it runs the task on a background thread. The second time, on the UI thread. Exact same code and parameters.