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); }
/// <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); } } } }
protected virtual void OnStateChanged(StateChangedEventArgs args) { StateChanged.Raise(this, args); }