locked
How to control the timeout for reading from StreamSocket? RRS feed

  • Question

  • Hello.

    I'm trying to write TCP read timeout handling () with StreamSocket and DataReader. With following sample, I expect await DataReaderLoadOperation will throw TaskCanceledException. But it does not throw any exception and await never completes.

    public static async Task<string> Get(Uri uri)
    {
    	StreamSocket socket = new StreamSocket();
    
    	try
    	{
    		await socket.ConnectAsync(new HostName(uri.Host), uri.Port.ToString(), SocketProtectionLevel.PlainSocket);
    	}
    	catch (Exception e)
    	{
    		return "Connect failed: " + e.ToString();
    	}
    
    	// Do not write request in order to to simulate read timeout situation
    
    	DataReader reader = new DataReader(socket.InputStream);
    	reader.InputStreamOptions = InputStreamOptions.Partial;
    	uint numBytesLoaded;
    	try
    	{
    		CancellationTokenSource cts = new CancellationTokenSource(1000); // cancel after 1000ms
    		DataReaderLoadOperation op = reader.LoadAsync(64 * 1024);
    		numBytesLoaded = await op.StartAsTask<uint>(cts.Token);
    	}
    	catch (TaskCanceledException tce)
    	{
    		return "Load timeout: " + tce.ToString(); // <= Expected path
    	}
    	catch (Exception e)
    	{
    		return "Load failed: " + e.ToString();
    	}
    	return "Success";
    }

    references:

    http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/b551f485-6a01-41c4-820f-578381addd93

    http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/4644b066-95b4-4bf8-bc7b-a434f9cee735

    Thursday, March 1, 2012 3:54 AM

Answers

  • Hi all - we've identified a bug in DataReaderLoadOperation, and will be filing this.  We don't have a workaround at this time.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator

    Wednesday, March 7, 2012 3:41 PM
    Moderator

All replies

  • Is this on DP or CP?  It looks like DP to me since DataReaderLoadOperation no longer has a "StartAsTask" method defined (according to http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.datareaderloadoperation.aspx#Y0).  If this is DP, can you try on CP and let us know what happens?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator

    Thursday, March 1, 2012 7:39 PM
    Moderator
  • It was on DP.

    Now I tried on Consumer Preview. I changed "StartAsTask" to "AsTask", then build success. But I get the same result (no exception, no return from await).

    Friday, March 2, 2012 6:30 AM
  • Hi Matt:

       I also get same result on CP version.


    Best Regards. Felix Long.

    Friday, March 2, 2012 7:56 AM
  • I'm investigating this week - I'll keep you updated.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator

    Tuesday, March 6, 2012 2:17 PM
    Moderator
  • Hi all - we've identified a bug in DataReaderLoadOperation, and will be filing this.  We don't have a workaround at this time.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator

    Wednesday, March 7, 2012 3:41 PM
    Moderator