Application hangs after a few minutes

Hi, sorry for posting many questions but I have no clue why and have investigated this thuroughly.

The issue does not happen with .NET 1.0 CF but with .NET 2.0 CF

 

My mobile smartphone application runs WM5. It has .NET 2.0 CF

The application simply connects to the server - thats all. nothing more, nothing less. Simple "connection" program.

When connecting, it creates a TcpClient object and of course if that is successful in establishing a connect - I create a Thread which listens to incoming communication.

 

Thats it.

 

Thing is, and it is frustrating that after a few minutes, the application seems to not respond at all. you can see slowly that it is degrading in performance - I have checked, put on debug breakpoints and what not - there is no thread being created over and over and over and over.......

The thread is only created once and that is only when the TcpClient was successful in establishing a connection.

 

I have no idea where it is or why it is causing the application to lock up after a few minutes of use.

I really dont know what to post but would appreciate some guidence from this small unfortunate problem.



private void DoJobs()
{
   if (this.DoConnectServer())
   {
      this.DoListenToIncomingData();
   }
}
private bool DoConnectServer()
{
   try
   {
       this.theTcpConnection = new TcpClient(this.servername, this.port);
       this.theNetworkStream = this.theTcpConnection.GetStream();
       return true;
    }
   catch (SocketException ex)
   {
      //error handling here
      return false;
   }
}
public void Listen()
{
   try
   {
      while(true)
      {
         if (this.theNetworkStream.DataAvailable)
         {
            byte[] incomingDataBuffer = new byte[256];
            int i = this.theNetworkStream.Read(incomingDataBuffer, 0, incomingDataBuffer.Length);
            //Do stuff here with the data such as parse strings or whatever
         }
      }
   }
   catch (ThreadAbortException ex)
   {
      //handle error
   }
}
private void DoListenToIncomingData()
{
   this.theListeningThread = new Thread(new ThreadStart(Listen));
   this.theListeningThread.IsBackground = true;
   this.theListeningThread.Start();
}

 

I hope it helps, that there is really the code. I do not know what else to say/post

Please advice me, I would greatly appreciate it :)




Answer this question

Application hangs after a few minutes

  • casey7

    You are doing

    while (true)

    {

    if (stream.DataAvailable) { .... }

    }

    this basically loops forever when no data is available eating all the CPU time.

    You don't need the if (stream.DataAvailable).... Just Read from the stream cause it will simply wait for data.


  • probie

    The while is OK. The if is the problem, simply remove it.

    while(true)
    {


    byte[] incomingDataBuffer = new byte[256];
    int i = this.theNetworkStream.Read(incomingDataBuffer, 0, incomingDataBuffer.Length);
    //Do stuff here with the data such as parse strings or whatever


    }


  • Reda

    Thanks.

    I have tried this and....it seems to work - interesting!

    Thanks once again, ill be sure to reply with any other issues I face of this if any but seems to be cured!



  • Kevin Moore - MSFT

    I thought this at one point, thank-you Mike. I appreciate this.

    Last question is - what do I do instead of the while() loop then I obviously want to keep looping instead of just exiting the method - or will it automatically execute this method (listen) when there is data waiting to be read

    what do I replace the while() with Surely you must need to keep running that loop so you can check at all times if there is data waiting to be read



  • Application hangs after a few minutes