Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
In C# how do I initiate a method to run postponed with ability to postpone further?

I need to run a delayed method in my code the way so that I could further control that "postponed method" (i.e. postpone it even more).

More specifically, I am doing this when reading data from serial port to ensure that the data has stopped flowing in. The data that I receive is rather unspecific so I can't rely on its length or some termination symbol. Instead, I want to use the DataReceived handler as usual and inside initiate the "process data" method as postponed for 200ms or so - but to postpone it again and again if further DataReceived handlers are firing.

So basically, my code would need to go something like this:

private string _serialPortCurrentReceivedDataBuffered = null;

private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    string receivedData = ((SerialPort)sender).ReadExisting();

    if (_serialPortCurrentReceivedDataBuffered == null)
    {
        _serialPortCurrentReceivedDataBuffered = receivedData;
    }
    else
    {
        _serialPortCurrentReceivedDataBuffered += receivedData;
    }

    <THAT POSTPONED METHOD>.SetStartToTime(DateTime.Now.AddMilliseconds(200));
}

private <THAT POSTPONED METHOD>()
{
    string receivedData = _serialPortCurrentReceivedDataBuffered;
    _serialPortCurrentReceivedDataBuffered = null;
    
    <HANDLE RECEIVED DATA>
}

Is there a way to do so? I am working with .NET Framework 4.7.2.

Also, inside <THAT POSTPONED METHOD> I will need to manipulate the UI (WinForms), if that's of any consideration in this case.

Thanks in advance!

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda