Click here to Skip to main content
12,403,812 members (72,986 online)
Click here to Skip to main content

Stats

31.2K views
758 downloads
136 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.Windows.Forms;
using ThreadBound;

namespace FormsTest
{
    public interface IUpdateInterface
    {
        void NewNumber(int number);
    }

    [ThreadBound(ThreadBoundAttribute.ThreadBinding.CurrentContext)]
    public partial class MainForm :
        Form,
        IUpdateInterface
    {
        private ThreadBound.IAsyncCallState callState;
        private NumberWorker numWorker;
        private NumberWorkerC numWorkerC;
        private IUpdateInterface threadBoundUpdater;

        public MainForm()
        {
            InitializeComponent();

            numWorker= new NumberWorker();
            numWorkerC = new NumberWorkerC();

            threadBoundUpdater = this.BindInterfaceToThread<IUpdateInterface>();

            numWorker.NewNumber += threadBoundUpdater.NewNumber;
            numWorkerC.NewNumber += threadBoundUpdater.NewNumber;
        }

        private void BtnStart_Click(object sender, EventArgs e)
        {
            numWorker.CreateNumbers(10, 500);
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            numWorker.Cancel();
        }

        public void NewNumber(int number)
        {
            LBNumbers.Items.Add(number.ToString());
        }

        private void BtnDispose_Click(object sender, EventArgs e)
        {
            numWorker.Dispose();
            numWorkerC.Dispose();
        }

        private void BtnStartState_Click(object sender, EventArgs e)
        {
            callState = numWorkerC.CreateNumbers(50, 100);
        }

        private void BtnCancelState_Click(object sender, EventArgs e)
        {
            if (callState != null)
                callState.Cancel();
        }
    }
}

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
Web02 | 2.8.160721.1 | Last Updated 13 Mar 2012
Article Copyright 2011 by gossd
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid