MGZ NetArt

Software Development and Support

Home
Design Patterns
C# Tips
MRDS Examples
Concurrent Execution
Task Activation
Iterator
Receivers and Arbiters
Resume
Contact Us
Here is a complete code for the Concurrent Execution example (pages 39 - 42 book). Just be sure that you have all that code in you main DSS Service Base class.
 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Threading;

using Microsoft.Ccr.Core;

using Microsoft.Dss.Core.Attributes;

using Microsoft.Dss.ServiceModel.Dssp;

using Microsoft.Dss.ServiceModel.DsspServiceBase;

using W3C.Soap;

using submgr = Microsoft.Dss.Services.SubscriptionManager;

namespace SpawnTest1

{

    [Contract(Contract.Identifier)]

    [DisplayName("SpawnTest1")]

    [Description("SpawnTest1 service (no description provided)")]

    class SpawnTest1Service : DsspServiceBase

    {

        /// <summary>

        /// Service state

        /// </summary>

        [ServiceState]

        SpawnTest1State _state = new SpawnTest1State();

        /// <summary>

        /// Main service port

        /// </summary>

        [ServicePort("/SpawnTest1", AllowMultipleInstances = true)]

        SpawnTest1Operations _mainPort = new SpawnTest1Operations();

        [SubscriptionManagerPartner]

        submgr.SubscriptionManagerPort _submgrPort = new submgr.SubscriptionManagerPort();

        /// <summary>

        /// Service constructor

        /// </summary>

        public SpawnTest1Service(DsspServiceCreationPort creationPort)

            : base(creationPort)

        {

        }

        /// <summary>

        /// Service start

        /// </summary>

        protected override void Start()

        {

            base.Start();

            // 1. Try this code first

            SimpleHandler();

            SimpleHandler();

            SimpleHandler();

            SimpleHandler();

            SimpleHandler();

            //2. And then try this one

            Spawn(SimpleHandler);

            Spawn(SimpleHandler);

            Spawn(SimpleHandler);

            Spawn(SimpleHandler);

            Spawn(SimpleHandler);

           // 3. Then You can try this one

            RunFromHandler();

        }

        private void SimpleHandler()

        {

            for (int i = 0; i < 10; i++)

            {

                Wait(100);

                Console.Write(i + " ");

            }

            Console.WriteLine("Finished Simple Handler Thread {0}",

            System.Threading.Thread.CurrentThread.ManagedThreadId);

        }

        //Do not forget to include this section (it is missing in a book!)

        private void Wait(int millisec)

        {

            //instead of:

            //Thread.Sleep(millisec);

            //use this (see p.72 of the book)

            AutoResetEvent signal = new AutoResetEvent(false);

            Activate(

            Arbiter.Receive(

            false,

            TimeoutPort(millisec),

            delegate(DateTime timeout)

            {

                signal.Set();

            }

            )

            );

            signal.WaitOne();

        }

        //Run a TAsk from a Handler

        void RunFromHandler()

        {

            Dispatcher d = new Dispatcher(3, "Test Pool");

            DispatcherQueue q = new DispatcherQueue("Test Queue", d);

            //Activaate FOUR task with a pool of THREE threads

            Arbiter.Activate(q,

            Arbiter.FromHandler(SimpleHandler),

            Arbiter.FromHandler(SimpleHandler),

            Arbiter.FromHandler(SimpleHandler),

            Arbiter.FromHandler(SimpleHandler));

        }

        /// <summary>

        /// Handles Subscribe messages

        /// </summary>

        /// <param name="subscribe">the subscribe request</param>

        [ServiceHandler]

        public void SubscribeHandler(Subscribe subscribe)

        {

            SubscribeHelper(_submgrPort, subscribe.Body, subscribe.ResponsePort);

        }

    }

}