Exemplo n.º 1
0
        private bool session()
        {
            Package result;
            Package stateResult;

            while (_isALive)
            {
                int start = Environment.TickCount;
                while (Environment.TickCount - start < 1500)
                {
                    if (_socket.Available != 0)
                    {
                        if (tryReceive(out result))
                        {
                            Received.Raise(this, new ReceiveEventArgs(result));
                        }
                    }
                    Thread.Sleep(100);
                }
                if (trySendAndReceive(new Package(3, Convert.ToByte('h'), null), out stateResult))
                {
                    if (BitConverter.ToInt16(stateResult.Data.ToArray(), 0) != _state)
                    {
                        StateChanged.Raise(this, new StateEventArgs(stateResult.Data));
                        _state = (ushort)BitConverter.ToInt16(stateResult.Data.ToArray(), 0);
                    }
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Change the state.
        /// </summary>
        public void SetState
        (
            bool newState
        )
        {
            Log.Trace
            (
                nameof(BusyState) + "::" + nameof(SetState)
                + ": newState="
                + newState
            );

            lock (_lock)
            {
                if (newState != _currentState)
                {
                    if (newState)
                    {
                        _waitHandle.Reset();
                        _thread = Thread.CurrentThread;
                    }
                    else
                    {
                        _waitHandle.Set();
                    }

                    _currentState = newState;

                    if (UseAsync)
                    {
                        StateChanged.RaiseAsync(this);
                    }
                    else
                    {
                        StateChanged.Raise(this);
                    }
                }
            }
        }
Exemplo n.º 3
0
 protected virtual void OnStateChanged(StateChangedEventArgs args)
 {
     StateChanged.Raise(this, args);
 }