C # Asynchronous Call Design

The requirement is to constantly read data from the hardware and send data to the upper layers for further processing.

I am currently using a synchronous mechanism. those. Request → Wait → Read ---> Send for processing.

    do
    {

    int ix = iy;
            iy = 0;

            if(ix<1)
            {
                while (((ix = ReadSst(ref sBuffer)) < 1) && bProcessNotEnded == true)
                {
                    if (ix < 0)    // BT Error
                    {
                        eErr = CError.BUFF_KILL; //Throw error and exit
                        break;
                    }

                    Thread.Sleep(2); 

                    iTimeOut += 2;
                    if (iTimeOut > TIMEOUT_2000)
                    {
                        eErr = CError.TIMEOUT_ERR;
                        objLogger.WriteLine("HW TIMED OUT");
                        break;
                    }

                }
            }

            iBytesRead = ix;

            if (iBytesRead <= 0)
            {
                eErr = CError.READ_ERR;
                objLogger.WriteLine("READ ERROR");
                break;
            }

            sReadMsg = sReadMsg + sBuffer;


    if(sReadMsg.Length >0)
        iEndPos = sReadMsg.IndexOf('\n',1);
    else
        iEndPos = -1;


                if (sReadMsg.Length > 2)
                {
                    if ((iEndPos == 0 && sReadMsg[0] == '\n') || (iEndPos > 0 && sReadMsg[sReadMsg.Length - 1] == '\n' && sReadMsg[sReadMsg.Length - 2] == '\r')) //finished
                    {
                        Thread.Sleep(50);  // wait a short time to be sure there that there is no further input
                        if (sReadMsg.Length >= 3 && sReadMsg[sReadMsg.Length - 3] == 'a')
                            continue;

                        iy = ReadSst(ref sBuffer);
                        if (iy == 0)
                        {
                            bProcessNotEnded = false;
                            objLogger.WriteLine("bProcessNotEnded is made false, End of frame detected");
                            break;
                        }
                        else if (iy < 0)
                        {
                            eErr = CError.BUFF_KILL;  // error
                            break;
                        }
                    }
                }


        } while (bProcessNotEnded); 

The ReadSst method is a call made by hardware to request data.

As you can see from the code, the true logic of the loop is and is read until the bProcessNotEnded flag is true. As soon as the end of the frame is detected in the line buffer that I get, I set the flag to false and the loop stops (just like reading from hardware).

, parallelism , , , .

-, .

+3
3

, .NET:

  • (.NET 4.0)

1 3, 2 .

, , , , ( sReadMsg) ( , ).

, TPL:

private void ReadMessageAsync()
{
    // Set up the task to read the message from the hardware device.
    Task<string> readMessageTask = new Task<string>(ReadMessage);

    // Set up the task to process the message when the message was read from the device.
    readMessageTask.ContinueWith(ProcessMessage);

    // Start the task asynchronously.
    readMessageTask.Start();
}

private void ProcessMessage(Task<string> readMessageTask)
{
    string message = readMessageTask.Result;

    // Process the message
}

private string ReadMessage()
{
    string message = string.Empty;

    // Retrieve the message using your loop

    return message;
}
+2

, -. , ReadSst, , - Stream. Read ( ) BeginRead ( ). . - , , , .

+1

All Articles