1

I have a while loop that begins when I press a button. I'm using it to maintain communication with a service on another computer. The problem is that when I get the "Completed" message back from the other computer I need it to break out of the while loop and stop listening till the button is pressed again. Nothing I do seems to break it out of the loop.

Please note the entire process is executed in its own thread.

I tried putting break; just before the end of the first case in the switch, no such luck and I'm not sure if it's because it's a switch statement that expects a break; between cases or what the reason is. I also tried putting return; in there but it still won't break out. I end up having to close the application and restart it to use the button again.

TcpClient client = new TcpClient(serverIP, 11000);
NetworkStream stream = client.GetStream();
Byte[] bytes = new Byte[256];
String data = null;
int i;

stream.Write(copy, 0, copy.Length);


while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
    //MessageBox.Show(data);
    switch (data)
    {
        case "Completed":
            this.Invoke((MethodInvoker)delegate
            {
                progressBar1.Value = 0;
                progressBar1.Visible = false;
                progressBar1.Update();
                if (prod)
                {
                    sqlLink.setProdFile(imageName, destFileName);
                } else
                {
                    sqlLink.setTestFile(imageName, destFileName);
                    if (sqlLink.getTestVM(imageName) != "")
                    {
                        if (message.Text("Test VM", "Power on specified Virtual Machine in private mode?", MessageBoxButtons.OKCancel) == DialogResult.OK)
                        {
                            PS ps = new PS();
                            ps.powerOnVM(sqlLink.getTestVM(imageName));
                        }
                    }
                }
                //Tried putting break; here.                            
            });
            break;
        case "FIU":
            {
                progressBar1.Value = 0;
                progressBar1.Visible = false;
                progressBar1.Update();
                message.Text("Error", "The image is in use. Try shutting down machines or unassigning devices.", MessageBoxButtons.OK);
            }
            break;
        case "DSF":
            {
                progressBar1.Value = 0;
                progressBar1.Visible = false;
                progressBar1.Update();
                message.Text("Error", "Drive space is full on production volume. Try deleting some older images.", MessageBoxButtons.OK);
            }
            break;
        default:
            this.Invoke((MethodInvoker)delegate
            {
                progressBar1.Value = Int16.Parse(data);
                progressBar1.Update();
            });
            break;
    }
 }
    stream.Close(); //This never happens.
    client.Close();
}
catch (Exception ex)
{
    message.Text("Error", "Copy Method Error: " + ex.Message, MessageBoxButtons.OK);
}
Nathan McKaskle
  • 2,926
  • 12
  • 55
  • 93

1 Answers1

5

Define a boolean variable that will hold the fact that you received the "Completed" message.

When you'll be entering the next iteration, if this value is true, then you break and you'll be getting out of your loop.

Example :


// Abbreviated
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
    bool breakTheWhile = false;
    switch (data)
    {
        case "Completed":
            // Abbreviated
            breakTheWhile = true;
            break;
        case "FIU":
            // Abbreviated
            break;
        case "DSF":
            // Abbreviated
            break;
        default:
            // Abbreviated
            break;
    }
    if (breakTheWhile)
        break;
 }

SynerCoder
  • 12,493
  • 4
  • 47
  • 78
Irwene
  • 2,807
  • 23
  • 48
  • This wont work correct, because you break in the next iteration meaning you will try to read again. Put the break beneath the switch statement – SynerCoder Jun 21 '16 at 13:45
  • Depend what is behind the stream, but not knowing that, you're correct , better be safe than sorry. – Irwene Jun 21 '16 at 13:46
  • So far this isn't working, I'm not sure why. I put a message box to prompt me telling me whether that conditional code block based on isFinished that you added actually gets executed but it does not, this is despite isFinished being set to true after it's completed just like you have above. – Nathan McKaskle Jun 21 '16 at 14:23
  • I think I know why it's not working. It's because the while loop is only executing the code block if the bytes do not equal 0. It needs another message to execute the break in the code. I may need to put it down at the bottom of the loop like @SnyderCoder suggests – Nathan McKaskle Jun 21 '16 at 14:29
  • Yeah none of this is working so far. Still trying to determine why. It should work but it doesn't. Adding in this code just breaks the whole thing entirely. – Nathan McKaskle Jun 21 '16 at 15:10
  • @NathanMcKaskle Did you try a step by step debugging yet ? – Irwene Jun 21 '16 at 15:12
  • Doing that now, trying to determine if it's the server side piece or what. – Nathan McKaskle Jun 21 '16 at 15:17
  • 1
    This works, turns out my issue was a disk full problem on the server and an event handler that wasn't initialized and so it wasn't informing me as it should have been. That said I think I need to seriously refactor the code. – Nathan McKaskle Jun 21 '16 at 16:13