c# - .NET async, can a single thread time-slice between tasks? -


i'm trying grasp on asynchronous programming in c#/.net. read article (link) on brown university's website cs168 course defines asynchronous programming interleaving tasks within same thread. says, "now can introduce asynchronous model... in model, tasks interleaved 1 another, in single thread of control", , shows interleaving in figure. can't seem 2 tasks interleave within same thread in .net. there way that?

i wrote simple apps try test theory, i'm not sure if i'm doing correctly. main program outputs screen every often, using thread.sleep() simulate work. asynchronous task same. if multiple threads used, output interleaved. i'm trying test on single thread.

i have wpf app runs on ui thread, task , main program output sequentially. create , start task this:

var taskfactory = new taskfactory(taskscheduler.fromcurrentsynchronizationcontext()); var task = taskfactory.startnew(workdelegate); 

i have console app starts task delegate using task.run(workdelegate);, runs them on different thread pool threads. i'm not sure how make them both run on same thread. if try same approach used in wpf runtime invalidoperationexception, "the current synchronizationcontext may not used taskscheduler".

multiple tasks won't automatically interleaved on single thread. that, have specify points in task code thread allowed cut on task. can via mechanism await task.yield. if you're running on single thread, thread not able allow other work progress unless explicitly yields.

when use taskscheduler start every task, message pump in wpf schedules each task run on ui thread, , run sequentially.

i have console app starts task delegate using task.run(workdelegate);, runs them on different thread pool threads. i'm not sure how make them both run on same thread.

you need install custom synchronizationcontext thread allowed post work thread.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -