I've got an interesting issue with a program I'm writing. I think I know kinda what is wrong, but I'm not sure about it, and I don't know how to fix it. I have the following code
.
.
.
string strCmd = "command";
OnDataSend(strCmd);
//wait for a response
while (!(m_szdata.Contains("OK\r\n")) && !(m_szdata.Contains("error")) &&
!(m_szdata.Contains("ERROR"))) ;
if (m_szdata.Contains("OK\r\n"))
{
btnLogin.Enabled = true;
OnUpdateServerStats();
}
else
MessageBox.Show("Error selecting server number.\r\n Please select another server.",
"ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OnUpdateServerStats()
{
string strCmd = "serverstats\r\n";
string strData;
string[] strParts, strSeps = { "\r\n", "=" };
OnDataSend(strCmd);
//Wait for a response
while (!(m_szdata.Contains("OK\r\n")) && !(m_szdata.Contains("error")) &&
!(m_szdata.Contains("ERROR"))) ;
strData = m_szdata;
m_szdata = "";
strParts = strData.Split(strSeps, StringSplitOptions.RemoveEmptyEntries);
string c = "";
foreach (string z in strParts)
c += z + Environment.NewLine;
MessageBox.Show(c, string.Format("{0} elements", strParts.Length));
}
Here's where I run in to problems. The program runs fine until it calls OnUpdataServerStats(). When it gets here it freezes and I know why (damn infinite loop). If I comment out the while statement it doesn't freeze, but my output shows 0 elements. (However I have all data received output to an edit box and all the data shows up there). Now if I take and comment out OnUpdataServerStats() and put it say in a button like the following
private void btnTest_Click(object sender, EventArgs e)
{
OnUpdateServerStats();
}
I get the proper number of elements and I can even leave the while statement in there and it doesn't lock up. I suspect it has something to do with the way the data is being received (which is Asynchronously), but I'm not sure how to fix that issue. Any suggestions?