All pastes #1978461 Raw Edit

Random Event Source for ZenStudi

public text v1 · immutable
#1978461 ·published 2010-11-01 05:24 UTC
rendered paste body
using System;
#if AVOID_GENERICS
using System.Collections;
using List_ReceiverPin = System.Collections.ArrayList;
using List_SenderPin = System.Collections.ArrayList;
using IEnumerable_SenderPin = System.Collections.IEnumerable;
using IEnumerable_ReceiverPin = System.Collections.IEnumerable;
using ConstantValueSource_bool = ZenStudio.Gui.ConstantValueSource;
using List_IDynamicLink = System.Collections.ArrayList;
using IEnumerable_IDynamicLink = System.Collections.IEnumerable;
using IEnumerable_IAttachedPropertyBag = System.Collections.IEnumerable;
#else
using System.Collections.Generic;
using System.Linq;
using List_ReceiverPin = System.Collections.Generic.List<ZenStudio.Gui.ReceiverPin>;
using List_SenderPin = System.Collections.Generic.List<ZenStudio.Gui.SenderPin>;
using IEnumerable_SenderPin = System.Collections.Generic.IEnumerable<ZenStudio.Gui.SenderPin>;
using IEnumerable_ReceiverPin = System.Collections.Generic.IEnumerable<ZenStudio.Gui.ReceiverPin>;
using ConstantValueSource_bool = ZenStudio.Gui.ConstantValueSource<bool>;
using List_IDynamicLink = System.Collections.Generic.List<ZenStudio.Gui.IDynamicLink>;
using IEnumerable_IDynamicLink = System.Collections.Generic.IEnumerable<ZenStudio.Gui.IDynamicLink>;
using IEnumerable_IAttachedPropertyBag = System.Collections.Generic.IEnumerable<ZenStudio.Gui.IAttachedPropertyBag>;
#endif
using System.Text;
using ZenStudio.Sensors;
using System.Threading;

namespace ZenStudio.Gui.Panda.Sensors
{
    class DebugTestSensor : IDebugTestSensor
    {
        string value;
        string Value { get { return value; } set { this.value = value; UpdatePins(); } }
        Thread bthread;
        public DebugTestSensor()
        {

            output = new SenderPin(this, "Value");
            Value = "???";
            bthread = new Thread(() =>
            {
                Random r = new Random();
                while (true)
                {
                    Thread.Sleep(1000);
                    Value = r.Next().ToString();
                }
            });
            bthread.Start();
        }
        SenderPin output;


        private void UpdatePins()
        {
            output.UpdateValue(Value);
        }

        public IEnumerable_SenderPin Outputs
        {
#if AVOID_YIELD
            get { return new SenderPin[] { output }; } // here we can yield one coz its const
#else
            get { yield return output; } // here we can yield one coz its const
#endif
        }

        public IEnumerable_ReceiverPin Inputs
        {
#if AVOID_YIELD
            get { return new ReceiverPin[] { }; }
#else
            get { yield break; }
#endif
        }


        public void ValueReceived(ReceiverPin input, object value)
        {
            throw new InvalidOperationException("Const cannot receive!");
        }


        public ReceiverPin GetInput(string name)
        {
            throw new NotImplementedException();

        }

        public SenderPin GetOutput(string name)
        {
            if (name != "Value") { throw new KeyNotFoundException(); }
            else { return output; }

        }
    }
}