Tim
Tim
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
aight, you've helped a lot already 😅, tysm! It's working now + you gave great feedback on some bad habits Good luck with work, I'll figure it out
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
Or is that because it goes through the Task binary
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
But it shouldn't say exception unhandled right?
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
I think I'm printing some detail..
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
Yeah I don't have to pass it
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
Test

Test 2
Collecting message...
Reading pipe async
[NORMAL]Discovering patches...
[NORMAL]No patches in this directory...
[NORMAL]Connecting...
[GOOD]Connected
Exception thrown: 'System.OperationCanceledException' in System.Core.dll
An exception of type 'System.OperationCanceledException' occurred in System.Core.dll but was not handled in user code
The operation was canceled.

Exception thrown: 'System.OperationCanceledException' in mscorlib.dll
Exception thrown: 'System.OperationCanceledException' in mscorlib.dll
Exception thrown: 'System.OperationCanceledException' in mscorlib.dll
Exception thrown: 'System.OperationCanceledException' in mscorlib.dll
Cancelled
Test

Test 2
Collecting message...
Reading pipe async
[NORMAL]Discovering patches...
[NORMAL]No patches in this directory...
[NORMAL]Connecting...
[GOOD]Connected
Exception thrown: 'System.OperationCanceledException' in System.Core.dll
An exception of type 'System.OperationCanceledException' occurred in System.Core.dll but was not handled in user code
The operation was canceled.

Exception thrown: 'System.OperationCanceledException' in mscorlib.dll
Exception thrown: 'System.OperationCanceledException' in mscorlib.dll
Exception thrown: 'System.OperationCanceledException' in mscorlib.dll
Exception thrown: 'System.OperationCanceledException' in mscorlib.dll
Cancelled
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
But before that it shows up as unhandled
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
Yes, I'm logging something when it catches:
c#
try
{
var msg = await _protocol.Receive(TimeSpan.FromSeconds(2));
Console.WriteLine(msg.Identifier);
}
catch (OperationCanceledException)
{
Console.WriteLine("Cancelled");
}
c#
try
{
var msg = await _protocol.Receive(TimeSpan.FromSeconds(2));
Console.WriteLine(msg.Identifier);
}
catch (OperationCanceledException)
{
Console.WriteLine("Cancelled");
}
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
I did in my even handler
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
Follow up question, why does it say unhandled when I'm catching it?
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
It cancels correctly after 2 seconds and the OperationCancelledException gets thrown as unhandled in user code
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
Using the CancelIoEx method instead
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
Ok, I have it working now
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
And then fails to ever end the read
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
It does get to the debug message
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
implemented like this
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
c#
private static Task<int> ReadPipeAsync(PipeStream pipe, byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested) return Task.FromCanceled<int>(cancellationToken);
var registration = cancellationToken.Register(() => CancelPipeIo(pipe));
var result = pipe.BeginRead(buffer, offset, count, null, null);
Console.WriteLine("Reading pipe async");
return Task.Run(() =>
{
try { return pipe.EndRead(result); }
finally { registration.Dispose(); }
}, cancellationToken);
}
c#
private static Task<int> ReadPipeAsync(PipeStream pipe, byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested) return Task.FromCanceled<int>(cancellationToken);
var registration = cancellationToken.Register(() => CancelPipeIo(pipe));
var result = pipe.BeginRead(buffer, offset, count, null, null);
Console.WriteLine("Reading pipe async");
return Task.Run(() =>
{
try { return pipe.EndRead(result); }
finally { registration.Dispose(); }
}, cancellationToken);
}
241 replies
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
doesn't throw after 2 seconds
241 replies