codingdir logo sitemap sitemap |

How to execute nested async/await code in parallel while maintaining the same thread on await continuations?

By : , Category : c#

You can create 4 independent threads, each one executes MethodA with a limited-concurrency (actually, no concurrency at all) TaskScheduler. That will ensure that every Task, and continuation Tasks, that the thread creates, will be executed by that thread.

    public void MyCallingMethod()
        CancellationToken csl = new CancellationToken();
        var threads = Enumerable.Range(0, 4).Select(p =>
                var t = new Thread(_ =>
                        Task.Factory.StartNew(() => MethodA(), csl, TaskCreationOptions.None,
                            new LimitedConcurrencyLevelTaskScheduler(1)).Wait();
                return t;
        //You can block the main thread and wait for the other threads here...

That won't ensure you a 4th degree parallelism, of course.

You can see an implementation of such TaskScheduler in MSDN -

ReLated :

Modify your configuration as follows:

<?xml version="1.0" encoding="utf-8"?>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="[version]" newVersion="[version]" />
        <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="[version]" newVersion="[version]" />

Replacing [version] with the version (for example, of System.Runtime and System.Threading.Tasks that you are referencing.

Your SendNotification has to return Task, by await keyword definition.

It's kind of javascript promise (just to give you an idea), which has to have done, failed functions, in order to be able track execution flow of async function.

Throttling is probably a much easier approach than prioritization.

I think your problem is best addressed by the TPL Dataflow library. It combines parallel and async technologies.

You can create "blocks" and "link" them together to form a "mesh" (in your case, the mesh is a pipeline). TransformBlock can be used with both synchronous and asynchronous actions, and also supports parallelism and throttling built-in.

Alternatively, you could apply asynchronous throttling to your ProcessData method using SemaphoreSlim (calling WaitAsync at the beginning of the method and Release at the end). But do consider TPL Dataflow; I find that if people are doing something this complex, then they usually find they can use TPL Dataflow in other parts of their application, too.

FOUND IT! Here Using FileSavePicker with MessageDialog's IUICommand event

Would have never thought that it had to do with the dialog box. To all who responded... Thanks for the help


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © All Rights Reserved .