locked
Translating Service Tutorial #2 to VB RRS feed

  • Question

  • I'm continuing to slog my way through the tutorials and trying to do it in VB. (VB may be a bad choice but I've been using it for years and it would really be hard to change).

     

    Here's the code I'm having a problem with in Service Tutorial #2

     

    protected override void Start()

    {

    base.Start();

    _timerPort.Post(DateTime.Now);

    Activate(Arbiter.Receive(true, _timerPort, TimerHandler));

    }

     

    void TimerHandler(DateTime signal)

    {

    _mainPort.Post(new IncrementTick());

    Activate(Arbiter.Receive(false, TimeoutPort(1000),delegate(DateTime time)

    {

    _timerPort.Post(time);

    }

    ));}

     

    I'm translating this a follows:

     

    Protected Overrides Sub Start()

    MyBase.Start()

    _timerPort.Post(DateTime.Now)

    Activate(Arbiter.Receive(True, _timerPort, TimerHandler))

    End Sub

     

    Public Sub TimerHandler(ByVal Signal As DateTime)

    _mainPort.Post(New IncrementTick)

    Activate(Arbiter.Receive(False, TimeoutPort(1000), AddressOf MyHandler))

    End Sub

    Private Sub MyHandler(ByVal Time As DateTime)

    _timerPort.Post(Time)

    End Sub

     

    This doesn't work because the argument for 'Signal' is not specified. I understand the error, but don't see how to fix it.

    Monday, September 1, 2008 4:42 PM

Answers

  • Yes, you beat me to it :-)

     

    You will also have to make some changes to the tick handler:

     

    Code Snippet

    <ServiceHandler(ServiceHandlerBehavior.Exclusive)> _

    Public Sub IncrementTickHandler(ByVal incrementTick As IncrementTick)

        _state.Ticks += 1

        LogInfo("Tick: " + _state.Ticks.ToString())

        incrementTick.ResponsePort.Post(DefaultUpdateResponseType.Instance)

    End Sub

     

     

    There is no equivalent to the C# yield statement in VB, so I have turned this into a Sub instead of a Function. In this particular case, the C# code simply has a yield break at the end so it does not need to be an Iterator.

     

    When you run the program you should see tick messages in the Immediate Window.

     

    Trevor

     

    Monday, September 1, 2008 8:20 PM

All replies

  • Well, never mind, I figured it out (I think). The critical line should be:

     

    Activate(Arbiter.Receive(True, _timerPort, AddressOf TimerHandler))

     

    Does that seem right?

    Monday, September 1, 2008 8:03 PM
  • Yes, you beat me to it :-)

     

    You will also have to make some changes to the tick handler:

     

    Code Snippet

    <ServiceHandler(ServiceHandlerBehavior.Exclusive)> _

    Public Sub IncrementTickHandler(ByVal incrementTick As IncrementTick)

        _state.Ticks += 1

        LogInfo("Tick: " + _state.Ticks.ToString())

        incrementTick.ResponsePort.Post(DefaultUpdateResponseType.Instance)

    End Sub

     

     

    There is no equivalent to the C# yield statement in VB, so I have turned this into a Sub instead of a Function. In this particular case, the C# code simply has a yield break at the end so it does not need to be an Iterator.

     

    When you run the program you should see tick messages in the Immediate Window.

     

    Trevor

     

    Monday, September 1, 2008 8:20 PM