Click here to Skip to main content
12,078,070 members (47,185 online)
Click here to Skip to main content

Stats

29.9K views
741 downloads
134 bookmarked
Posted

Declarative multithreading

, 13 Mar 2012 CDDL
An introduction and proof of concept code for the idea of declarative multi threading in C#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadBound
{
    class PoolSynchronizationContext :
        SynchronizationContext
    {
        public PoolSynchronizationContext()
        {
        }

        public override void Post(SendOrPostCallback d, object state)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(d.Invoke), state);
        }

        public override void Send(SendOrPostCallback d, object state)
        {
            //-- Use the SyncWorkQueueItem to be able to wait for the call to finish
            SyncWorkQueueItem syncQueueItem=new SyncWorkQueueItem(d, state);

            ThreadPool.QueueUserWorkItem(new WaitCallback(syncQueueItem.Invoke), null);

            //-- Auf das Ende der Operation warten und ggf. Exceptions erneut werfen
            syncQueueItem.Join();
            syncQueueItem.RethrowException();
        }

        public override SynchronizationContext CreateCopy()
        {
            return new PoolSynchronizationContext();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)

Share

About the Author

gossd
Systems Engineer
Germany Germany
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160212.1 | Last Updated 13 Mar 2012
Article Copyright 2011 by gossd
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid