/// <summary> /// Запустить бесконечный цикл прослушивания /// </summary> /// <returns></returns> private async Task RunListen() { try { while (isRunListen) { string messageReceive = await myPipeClientServer.Listen(); if (messageReceive == COMMAND_STOP) { isRunListen = false; return; } DataUpdated?.Invoke(this, new DataUpdatedEventArgs <string>(new NetworkVariableData <string>(messageReceive))); } } catch (Exception ex) { // ошибка возникает при закрытии слушающего ожидающего подключения, // которая перехватывается здесь Debug.WriteLine("ERROR: {0}", ex.ToString()); WriteCompleted?.Invoke(this, new WriteCompletedEventArgs(ex, false, this)); } }
/// <summary> /// Method that gets executed when a write request has been completed. /// </summary> protected virtual void WriteCompletedHandler() { //This handler is invoked by IO threads //Make it quick WriteCompleted?.Invoke(); //There is no need for synchronization here //Only 1 thread can be here at the same time. //Set the idle timeout to avoid idle disconnects if (_heartBeatInterval > 0 && !_isCanceled) { try { _idleTimer.Change(_heartBeatInterval, Timeout.Infinite); } catch (ObjectDisposedException) { //This connection is being disposed //Don't mind } } Interlocked.CompareExchange(ref _writeState, Connection.WriteStateInit, Connection.WriteStateRunning); //Send the next request, if exists //It will use a new thread RunWriteQueue(); }
/// <summary> /// Асинхронная отправка команды /// </summary> /// <param name="commandValueWriter"></param> /// <returns></returns> public async Task WriteDataAsync(string commandValueWriter) { if (IsControlCommadVisible) { UserControlCommandTarget.SetButtonSendEnabled(false); UserControlCommandTarget.UpdateError(""); } try { await myPipeClientServer.SendAsync(commandValueWriter, 1000); } catch (Exception ex) { Debug.WriteLine("ERROR: {0}", ex.ToString()); //Await Task.Run(Function() Console.WriteLine("ERROR: {0}", ex.ToString)) if (IsControlCommadVisible) { UserControlCommandTarget.UpdateError(ex.ToString()); } ErrorCommand = ex.ToString(); WriteCompleted?.Invoke(this, new WriteCompletedEventArgs(ex, false, this)); } if (IsControlCommadVisible) { UserControlCommandTarget.UpdateSendTextBox(commandValueWriter); UserControlCommandTarget.SetButtonSendEnabled(true); } }
/// <summary> /// Handles the send completed event /// </summary> protected void OnSendCompleted(object sender, SocketAsyncEventArgs e) { if (e.SocketError != SocketError.Success) { OnError(null, e.SocketError); } OnWriteFlushed(); WriteCompleted?.Invoke(); }
/// <summary> /// Handles the continuation for WriteAsync faulted or Task on Stream mode /// </summary> protected void OnSendStreamCallback(Task writeTask) { if (writeTask.Exception != null) { writeTask.Exception.Handle(_ => true); HandleStreamException(writeTask.Exception.InnerException); return; } OnWriteFlushed(); WriteCompleted?.Invoke(); }
private void writeCallback(IAsyncResult ar) { try { netStream.EndWrite(ar); WriteCompleted?.Invoke(this, new NetworkMessageWriterWriteCompletedEventArgs(ar.AsyncState as NetworkMessage, TcpClient)); } catch (Exception ex) { WriteError?.Invoke(this, new NetworkMessageWriterWriteErrorEventArgs(ar.AsyncState as NetworkMessage, TcpClient, ex)); } }
public void WriteBytes(string fileName, byte[] dataSet, float percentageToFireEvent) { var lenght = dataSet.Length; for (int i = 0; i < lenght; i++) { File.AppendAllText(fileName, dataSet[i].ToString() + Environment.NewLine); if (i % (percentageToFireEvent * 100.0f) == 0) { WritePerformed?.Invoke(this, new WritePerformedEventArgs(dataSet)); } } WriteCompleted?.Invoke(this, EventArgs.Empty); }
private void writeCallback(IAsyncResult ar) { try { netStream.EndWrite(ar); WriteCompleted?.BeginInvoke(this, new NetworkMessageWriterWriteCompletedEventArgs(ar.AsyncState as NetworkMessage, TcpClient), null, null); } catch (Exception ex) { WriteError?.BeginInvoke(this, new NetworkMessageWriterWriteErrorEventArgs(ar.AsyncState as NetworkMessage, TcpClient, ex), null, null); if (OnErrorStopWritingAndCloseClient) { netStream?.Close(); TcpClient?.Close(); WriteCompleted = null; WriteError = null; } } }
protected virtual void OnWriteCompleted(EventArgs e) { WriteCompleted?.Invoke(this, e); }