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!