C# Wait until data received and go to next iteration
<p>I'm using Serial Port to get data from device in for loop iteration.
The problem is in loop iteration i need to get data from serial port, validate it and going to the next iteration.
How i can achieved this?</p>

<p>Here are my code :</p>

<pre><code>private void processData()
{
    // Loop Procedure 
    int x = Int32.Parse(master["Cycle"].ToString());
    int y = Int32.Parse(master["MinWeight"].ToString());

    // Loop for each line
    for (int i = this.CLine; i &lt; 2; i++)
    {
        this.CLine = i;
        if (i == 0)
            label15.Text = master["LLINE"].ToString();
        else
            label15.Text = master["RLINE"].ToString();

        IDictionary&lt;string, string&gt; dic = (Dictionary&lt;String, String&gt;)master[i.ToString()];
        label18.Text = this.CProcess = dic["PROCESSID"];
        int z = Int32.Parse(dic["PRODLANE"].ToString());

        // Loop for each sampling session (Cycle)
        for (int j = this.CCycle; j &lt;= x; j++)
        {
            this.CCycle = j; 

            // Loop for production lane
            for (int k = this.CLane; k &lt;= z; k++)
            {
                this.CLane = k;
                label16.Text = k.ToString(); 


                // In this section i want to send command over serial port
                // get value from my device
                // validate it if current weight bellow standard weight
                // do it again (get data from device)
                // else we can go to next iteration

                while (this.CWeight &lt; y)
                {
                    XApi.l("xxx2 " + this.CWeight + " vs " + y + " " + k.ToString() + " " + this.isDataReady); 
                    SendData("Q"); 
                }

                // Commit Transaction
                // XDb.CommitTrans(this.CCycle.ToString(), dic["LINEID"].ToString(), this.CLane.ToString(), weight.ToString(), this.isTrialStage == true ? "1" : "0");   
            }
        }
    }
}
</code></pre>

<p>I've tried this</p>

<pre><code>while (this.CWeight &lt; y)
{
    XApi.l("xxx2 " + this.CWeight + " vs " + y + " " + k.ToString() + " " + this.isDataReady); 
    SendData("Q"); 
}
</code></pre>

<p>but it seems blocked UI thread and make my application solaggy.
Anyone can give me some idea? Thanks in advance.</p>

<pre><code>private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    if (e.EventType == System.IO.Ports.SerialData.Eof) 
        return;

    // If the com port has been closed, do nothing
    if (!comport.IsOpen) 
        return;

    // Update flag data received
    this.isDataReady = true;

    // Determain which mode (string or binary) the user is in
    if (CurrentDataMode == DataMode.Text)
    {
        // Read all the data waiting in the buffer
        string data = comport.ReadExisting();

        // Update result        
        result += data;

        if (result.Length &gt; 16)
        { 
            SetText(result.ToString());
        }

        // Display the text to the user in the terminal
        Log(LogMsgType.Incoming, data);
    }
    else
    {
        // Obtain the number of bytes waiting in the port's buffer
        int bytes = comport.BytesToRead;

        // Create a byte array buffer to hold the incoming data
        byte[] buffer = new byte[bytes];

        // Read the data from the port and store it in our buffer
        comport.Read(buffer, 0, bytes);

        // Show the user the incoming data in hex format
        Log(LogMsgType.Incoming, ByteArrayToHexString(buffer));
    }
}

private void SendData(String msg)
    {
        this.isDataReady = false;
        result = "";
        if (CurrentDataMode == DataMode.Text)
        {
            // Send the user's text straight out the port
            comport.Write(msg + "\r\n");

            // Show in the terminal window the user's text
            Log(LogMsgType.Outgoing, msg + "\n");
        }
        else
        {
            try
            {
                // Convert the user's string of hex digits (ex: B4 CA E2) to a byte array
                byte[] data = HexStringToByteArray(txtSendData.Text);

                // Send the binary data out the port
                comport.Write(data, 0, data.Length);

                // Show the hex digits on in the terminal window 
                Log(LogMsgType.Outgoing, ByteArrayToHexString(data) + "\n"); 
            }
            catch (FormatException)
            {
                // Inform the user if the hex string was not properly formatted
                Log(LogMsgType.Error, "Not properly formatted hex string: " + txtSendData.Text + "\n");
            }
        } 
    }
</code></pre>