MGZ NetArt

Software Development and Support

Home
Design Patterns
C# Tips
MRDS Examples
Concurrent Execution
Task Activation
Iterator
Receivers and Arbiters
Resume
Contact Us

using System;

using System.Collections.Generic;

using System.ComponentModel;

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;

using System.Threading;

namespace SpawnTest // Do not forget to change that namespace accordingly

{

[Contract(Contract.Identifier)]

[DisplayName("SpawnTest")]

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

[ActivationSettings(ShareDispatcher=false, ExecutionUnitsPerDispatcher=6)]

class SpawnTestService : DsspServiceBase

{

/// <summary>

/// Service state

/// </summary>

[ServiceState]

SpawnTestState _state = new SpawnTestState();

/// <summary>

/// Main service port

/// </summary>

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

SpawnTestOperations _mainPort = new SpawnTestOperations();

[SubscriptionManagerPartner]

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

/// <summary>

/// Service constructor

/// </summary>

public SpawnTestService(DsspServiceCreationPort creationPort)

: base(creationPort)

{

}

/// <summary>

/// Service start

/// </summary>

protected override void Start()

{

base.Start();

Receive();

}

void Receive()

{

Port<int> intPort = new Port<int>();

//Create NOT Persistent Receiver

Activate(

Arbiter.Receive(false, intPort,

delegate(int n)

{Console.WriteLine("Receiver 1: " + n.ToString()); }

)

);

//Create a persistent Receiver

Activate(

Arbiter.Receive(true, intPort,

delegate(int n)

{ Console.WriteLine("Receiver 2: " + n.ToString()); }

)

);

Wait(100);

Console.WriteLine("Receivers Activated:\n" + intPort.ToString());

//Post a message

intPort.Post(10);

Wait(100);

Console.WriteLine("After First Post:\n" + intPort.ToString());

//Wait a wile and Post another message

Wait(100);

intPort.Post(-2);

Console.WriteLine("After Second Post:\n" + intPort.ToString());

Wait(100);

intPort.Post(123);

Wait(100);

}

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();

}

}

}