I've got something I need to do and I'm familiar with GCD so I know how to do what I want there. The problem being that I need to do this in C# (VS2013) and I pretty much never use C#. I've paid enough attention to it so I know about things like async/await but this and other things I've found via Google don't do quite what I want as far as I can tell.
Basically I have a bunch of tasks which I need to execute serially, and I want them to occur asynchronously in the background. But the main thread cannot wait while, for example, a BackgroundWorker.IsBusy(): the tasks must simply queue up in the background while the main thread continues on doing other things.
Since I'm only creating background tasks from a single thread it looks like I could do something like:
Is there a better way? Am I right that this wouldn't work with multiple threads queuing tasks?
Basically I have a bunch of tasks which I need to execute serially, and I want them to occur asynchronously in the background. But the main thread cannot wait while, for example, a BackgroundWorker.IsBusy(): the tasks must simply queue up in the background while the main thread continues on doing other things.
Code:
dispatch_queue_t background = dispatch_queue_create("background", DISPATCH_QUEUE_SERIAL);
dispatch_async(background, ^{
/* task which I don't want to block my main
thread, but which must execute serially
with other background tasks */
});
OtherStuff();
dispatch_async(background, ^{ /* ... */ });
OtherStuff();
dispatch_async(background, ^{ /* ... */ });
OtherStuff();
Since I'm only creating background tasks from a single thread it looks like I could do something like:
Code:
var background = new Task(() => {});
background.Start();
background = background.ContinueWith(t => { /* ... */ });
OtherStuff();
background = background.ContinueWith(t => { /* ... */ });
OtherStuff();
Is there a better way? Am I right that this wouldn't work with multiple threads queuing tasks?