0

I am writing a .NET Compact Framework application for a Windows CE device. My application communicates with a desktop computer using XML serialized data packets.

What I am encountering is that if the network cable becomes unplugged, my stream.Read operation blocks indefinitely. Setting a timeout is not supported in my CF.

I am looking for a "best practices" answer on how to deal with this issue. If the connection becomes closed for any reason, my loop should stop and begin trying to reconnect.

I have found this answer but it does not include the "using" directives that I need, and I am not sure what is going on anyway.

All tips are appreciated, especially if they solve the problem. Thanks!

Community
  • 1
  • 1
user2434237
  • 21
  • 1
  • 5
  • You didn't include any kind of code sample...what stream class are you using? You may have better luck working directly with the Socket class. – tcarvin Feb 12 '14 at 13:18

1 Answers1

0

As stream.Read is a blocking call you should move the code into a separate thread as recommended for all blocking functions.

You can then use the linked code to stop the thread. For example using a Thread.Join first and then a Thread.Abort(). Put your code inside the thread within a try/catch block and catch the ThreadAbortException to know when the thread has been aborted.

As you start writing threaded code, you have to find a way to sync with the rest of your code. This is normally done using delegates and event handlers. But this is a different task and worth some research if you did not d this before.

Community
  • 1
  • 1
josef
  • 5,951
  • 1
  • 13
  • 24