Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Declarative multithreading

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

namespace ThreadBound
{
    /// <summary>
    /// Synchronization context for the worker thread context. If a class is bound to a thread via the
    /// [ThreadBound]-attribute the class will be executed within its own worker thread. This class
    /// implements the synchronization context for the Worker class. It will be automatically created
    /// if a Worker instance is created.
    /// </summary>
    internal class WorkerSynchronizationContext :
        SynchronizationContext,
        IDisposable
    {
        /// <summary>
        /// Reference to the worker that is used for executing the methods.
        /// </summary>
        private Worker AssignedWorker;

        /// <summary>
        /// C'tor
        /// </summary>
        /// <param name="workerToUse">Reference to the worker that will execute the callbacks passed to this synchronization context.</param>
        public WorkerSynchronizationContext(Worker workerToUse)
        {
            AssignedWorker = workerToUse;
        }

        /// <summary>
        /// Overrides the Post method in SynchronizationContext.
        /// </summary>
        /// <see cref="SynchronizationContext.Post"/>
        public override void Post(SendOrPostCallback d, object state)
        {
            AssignedWorker.EnqueAsync(d, state);
        }

        /// <summary>
        /// Overrides the Send method in SynchronizationContext.
        /// </summary>
        /// <param name="d"></param>
        /// <see cref="SynchronizationContext.Send"/>
        public override void Send(SendOrPostCallback d, object state)
        {
            AssignedWorker.EnqueSync(d, state);
        }

        /// <summary>
        /// Overrides the CreateCopy of the SynchronizationContext.
        /// </summary>
        /// <see cref="SynchronizationContext.SynchronizationContext"/>
        public override SynchronizationContext CreateCopy()
        {
            return new WorkerSynchronizationContext(AssignedWorker);
        }

        /// <summary>
        /// Wird der Synchronisationskontext abgebaut, so wird der zugewiesene Worker angehalten.
        /// </summary>
        public void Dispose()
        {
            AssignedWorker.Stop();
        }
    }
}

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

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