title: Program runs for some time then stops executing (console window is still running with the cursor blinking) without giving out any error
body: <p>I am new to programming and trying to write a simple program that reads data from a modbus device as long as device is connected. If the device is disconnected, it displays an error and keeps trying to connect until the device is connected again.
  The issue is that this console program will run for some time, connect to the device and log correct data to a file called "filename". But after running for like 10-20 mins (its different everytime I run it), the console program just stops writing anything either on the console window or the file witout displaying any error. The device is still connected since the program doesnt give any error.
  The console window keeps running without any error but the program execution is stopped.
  I would really like to understand why that problem exists and how to solve it.
  I am using visual studio 2013.</p>
  
  <pre><code>namespace ReadData
  {
  class Program
  {
      static void Main(string[] args)
      {
          //initialize variables
          TextWriter file = new StreamWriter(filename, true); //file to write data
  
          //create a new instance of device called slave.
  
          while (!slave.Connected)
          {
              try
              {
                 //try to connect
              }
              catch (SocketException)
              {
                 //display error
              }
  
          while (slave.Connected) //keep getting data as long as device is connected
          {
              try
              {
  
                  //read data from device
                  //write to the file every minute
                  file.Flush();
                  Thread.Sleep(1000);
  
              }
              catch (IOException)
              {
                  //display error and try to connect again once
              }
              catch (IndexOutOfRangeException)
              {
                  //display error and disconnect device
              }
          }
        }
      }
    }
  }
  </code></pre>