locked
Make a timeout for tcpReception

    Question

  • Hello, I work on an windows universal app project and I receive data from a server but when I use the await LoadAsync method and all the data has been received the method enters in an infinite loop ,so I want to make a timeout, to do this I use a cancellationTokenSource but when the timeOut occur it close the socket, is it normal ?

     CancellationTokenSource cts = new CancellationTokenSource();
     try
           {
       cts.CancelAfter(2000);
      await reader.LoadAsync(sizeof(UInt16)).AsTask(cts.Token);
      cts.Cancel();
                                                
              }
             catch (TaskCanceledException a){
            Debug.WriteLine("error");        
    
         }      

    Friday, August 22, 2014 8:59 AM

Answers

  • Hi raicas,

    Looks correct from your code, but why will you cancel the task again after you schedule the cancel after 2000ms? See this for more information: How to set timeouts on socket operations

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, August 25, 2014 7:05 AM
    Moderator