You could use a Thread
or you could use Task
TPL. There are also some events you can utilize on the SerialPort class such as DataReceived
How about changing it from Read
to BeginRead
. You you can then use AsyncCallback
.
byte[] received = new byte[port.BytesToRead];
result = port.BaseStream.BeginRead(received
, 0, port.BytesToRead, new AsyncCallback(ReadCallBack), port.BaseStream);
private void ReadCallBack(IAsyncResult ar)
{
Stream stream = (Stream)ar.AsyncState;
// Do reading here?
}