private async Task <IXBeeResponse> SendCommandAsync(IXBeeCommand command, CancellationToken cancellationToken) { IXBeeListener xbeeListener = new XBeeListener(); AddTransactionListener(xbeeListener); lock (_frameIdLock) { xbeeListener.OurFrameId = Interlocked.Increment(ref _frameId); command.SetFrameId(xbeeListener.OurFrameId); Interlocked.CompareExchange(ref _frameId, 1, 256); } // Send the transaction QueueFrame(command); while (!_mainCancellationToken.IsCancellationRequested && !cancellationToken.IsCancellationRequested && !xbeeListener.Complete) { // wait until transaction is complete await Task.Delay(5); } RemoveTransactionListener(xbeeListener); return(xbeeListener.CompletionResponse); }
/// <summary> /// Sends a XBee request to the NCP without waiting for the response. /// </summary> /// <param name="command">Request <see cref="IXBeeCommand"/> to send.</param> /// <returns></returns> public async Task <IXBeeResponse> SendRequestAsync(IXBeeCommand command) { IXBeeListener xbeeListener = new XBeeListener(); await _taskFactory.StartNew(() => { AddTransactionListener(xbeeListener); lock (_frameIdLock) { xbeeListener.OurFrameId = Interlocked.Increment(ref _frameId); command.SetFrameId(xbeeListener.OurFrameId); Interlocked.CompareExchange(ref _frameId, 1, 256); } // Send the transaction QueueFrame(command); lock (xbeeListener) { while (!xbeeListener.Complete) { // wait until transaction is complete } } RemoveTransactionListener(xbeeListener); }); return(xbeeListener.CompletionResponse); }