Do not need to pause the Thread to bring the results

0

I have a problem getting data from a device.

It works like this: this device is called Module , in this Module I can start some operations like start , stop , abort operation ...

The application I am working on, sends commands to this module and also receives information, to send the commands, is already working, my problem is when I am querying the data of this Module, I need to pause Thread in 2 (seconds), if I do not pause it does not work.

How can I improve this method so I do not need to pause the Thread, and only return when I have information.

Code:

    private static NetworkStream _stream;
    private static DescargaTolflux DescargaTolflux { get; set; }

    public async Task CapturarInfoDoModulo()
    {
        byte[] _capturarInformacao;

        var _tcpClientBalanca = new TcpClient("IP", 7000);

        _stream = _tcpClientBalanca.GetStream();

        if (!_stream.DataAvailable)
            DescargaTolflux = null;

        _capturarInformacao = new byte[_tcpClientBalanca.ReceiveBufferSize];

        await Task.Run(() =>
        {
            _stream.BeginRead(_capturarInformacao, 0, _capturarInformacao.Length, new AsyncCallback(TolfluxCallback), _capturarInformacao);
            Thread.Sleep(2000);
        });
    }

    private void TolfluxCallback(IAsyncResult iResult)
    {
        int tam = _stream.EndRead(iResult);

        byte[] buffer = (byte[])iResult.AsyncState;
        var resultado = Encoding.ASCII.GetString(buffer, 0, buffer.Length);
        Char limitador = '|';
        String[] dadosArray = resultado.Split(limitador);

        var data = DateTime.Parse(dadosArray[0].Replace("\u000282SP", ""));
        var hora = TimeSpan.Parse(dadosArray[1]);

        var tolflux = new DescargaTolflux();

        tolflux.Data = data + hora;
        tolflux.NumeroTerminal = dadosArray[2];
        tolflux.Produto = dadosArray[3];
        tolflux.NumeroSubtotalizador = dadosArray[4];
        //tolflux.Cacambada = Int32.Parse(dadosArray[5]);
        tolflux.PesoBruto = Int32.Parse(dadosArray[6]);
        tolflux.PesoTara = Decimal.Parse(dadosArray[7]);
        tolflux.FluxoTonelada = Decimal.Parse(dadosArray[8]);
        tolflux.AcumuladoSubtotalizador = Decimal.Parse(dadosArray[9]);
        tolflux.TotalGeral = Decimal.Parse(dadosArray[10]);
        tolflux.TipoMensagem = dadosArray[11];

        DescargaTolflux = new DescargaTolflux();
        DescargaTolflux = tolflux;
    }

I created a DownloadTolflux static object to store the result and return the values to my screen.

If it is not clear, comment that I edit the question.

    
asked by anonymous 22.02.2018 / 18:49

1 answer

0

With the help of @Gabriel Coletta in the comments, I was able to solve it in a better way:

public async Task CapturarInfoDoModulo()
{
    byte[] _capturarInformacao;

    var _tcpClientBalanca = new TcpClient("IP", 7000);

    _stream = _tcpClientBalanca.GetStream();

    if (!_stream.DataAvailable)
        DescargaTolflux = null;

    _capturarInformacao = new byte[_tcpClientBalanca.ReceiveBufferSize];

    var result = Task.Run(() =>
    {
        _streamBalanca1.BeginRead(_capturarInformacao, 0, _capturarInformacao.Length, new AsyncCallback(PesoCallbackBalanca1), _capturarInformacao).AsyncWaitHandle.WaitOne();
    });

    await result;
}

Microsoft references: NetworkStream , WaitHandle.WaitOne

    
22.02.2018 / 21:19