UWP StreamSocket ConnectAsync exception 0xC00D36FF

家住魔仙堡 提交于 2019-12-12 02:16:31

问题


I'm getting a very strange exception using a UWP StreamSocket, 99.9% of the time this code functions as expected and the communication with the device works properly, however I get the following exception every once in a while.

The exception message:

The operation failed because an invalid combination of workqueue ID and flags was specified. (Exception from HRESULT: 0xC00D36FF)

Sample code for the issue:

using (StreamSocket analyzerSocket = new StreamSocket())
{
    HostName hostName = new HostName(host);

    // Set NoDelay to false so that the Nagle algorithm is not disabled
    analyzerSocket.Control.NoDelay = false;

    try
    {
        // Connect to the server
        await analyzerSocket.ConnectAsync(hostName, port.ToString()).AsTask(new CancellationTokenSource(_timeout).Token);
    }
    catch (Exception e)
    {
        var x = e;
    }
}

Screenshot of exception in code:

The Stack Trace:

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Analyzer.<SendMessageAsync>d__120.MoveNext() in zzz.cs:line 779

I've tried my GoogleFu and I was able to find issues with MediaPlayer or Linux kernels but nothing seemed to relate to this issue with StreamSockets.

While I can trap this error and work around the issue I would like to know what's going on in case it's a symptom of a bigger issue.

Thanks in advance.

Edit 1

I thought this might be related to http://referencesource.microsoft.com/#mscorlib/system/runtime/compilerservices/TaskAwaiter.cs,be57b6bc41e5c7e4 based on the code comment of "// And throw an exception if the task is faulted or canceled.".

However I still get this expcetion when I am not using the ".AsTask(new CancellationTokenSource(timeout.Value).Token)"

Edit 2

When I have this in a loop, to constantly send messages to our device, the messages are being received until this exception occurs. Once the exception occurs and I tell it to continue and try again, the exception re-occurs over and over in the loop and the device stops receiving messages.

Edit 3

So I've tried the following, to connect to a different device, with a different instance of the StreamSocket object... and it generates the same error!

using (StreamSocket analyzerSocket = new StreamSocket())
{
    HostName hostName = new HostName(host);

    // Set NoDelay to false so that the Nagle algorithm is not disabled
    analyzerSocket.Control.NoDelay = false;

    try
    {
        // Connect to the server
        await analyzerSocket.ConnectAsync(hostName, port.ToString()).AsTask(new CancellationTokenSource(_timeout).Token);
    }
    catch (Exception e)
    {
        using (StreamSocket analyzerSocket2 = new StreamSocket())
        {
            HostName hostName2 = new HostName("xxx.xxx.xxx.xxx");

            // Set NoDelay to false so that the Nagle algorithm is not disabled
            analyzerSocket.Control.NoDelay = false;

            // Connect to the server
            await analyzerSocket2.ConnectAsync(hostName2, port.ToString());
        }

        throw;
    }
}

It feels like some sort of cross threading type of issue... I'm grasping at straws right now as I cannot trap and bypass the error as once the error occurs I can no longer talk to the devices and I must exit the application to get it to work again.

Does anyone have any other ideas or confirmation that it looks like cross threading type of issue?

Thanks in advance.


回答1:


The only way I have found to prevent this issue is to stop using the "StreamSocket" altogether.

I switched my code to use the "System.Net.Sockets" namespace using the Socket object instead and with some modifications to my code I haven't encountered this issue since.

Code sample:

https://msdn.microsoft.com/en-us/library/windows/apps/hh202858%28v=vs.105%29.aspx?f=255&MSPPError=-2147217396



来源:https://stackoverflow.com/questions/39711444/uwp-streamsocket-connectasync-exception-0xc00d36ff

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!