cancellation-token

Async network operations never finish

夙愿已清 提交于 2019-11-26 17:56:37
I have several asynchronous network operations that return a task that may never finish: UdpClient.ReceiveAsync doesn't accept a CancellationToken TcpClient.GetStream returns a NetworkStream that doesn't respect the CancellationToken on Stream.ReadAsync (checking for cancellation only at the start of the operation) Both wait for a message that may never come (because of packet loss or no response for example). That means I have phantom tasks that never complete, continuations that will never run and used sockets on hold. I know i can use TimeoutAfter , but that will only fix the continuation

GetResponseAsync does not accept cancellationToken

吃可爱长大的小学妹 提交于 2019-11-26 11:29:40
问题 It seems that GetResponseAsync does not accept cancellationToken in Async/Await. So the question is how can I cancel the below procedure, provided I need to collect Cookies from response: using (HttpWebResponse response = (HttpWebResponse) await request.GetResponseAsync()) { cookies.Add(response.Cookies); } An alternative code to achieve the above is also welcome. 回答1: Something like this should work (untested): public static class Extensions { public static async Task<HttpWebResponse>

NetworkStream.ReadAsync with a cancellation token never cancels

孤街醉人 提交于 2019-11-26 10:50:05
问题 Here the proof. Any idea what is wrong in this code ? [TestMethod] public void TestTest() { var tcp = new TcpClient() { ReceiveTimeout = 5000, SendTimeout = 20000 }; tcp.Connect(IPAddress.Parse(\"176.31.100.115\"), 25); bool ok = Read(tcp.GetStream()).Wait(30000); Assert.IsTrue(ok); } async Task Read(NetworkStream stream) { using (var cancellationTokenSource = new CancellationTokenSource(5000)) { int receivedCount; try { var buffer = new byte[1000]; receivedCount = await stream.ReadAsync

Cancellation token in Task constructor: why?

寵の児 提交于 2019-11-26 10:13:32
Certain System.Threading.Tasks.Task constructors take a CancellationToken as a parameter: CancellationTokenSource source = new CancellationTokenSource(); Task t = new Task (/* method */, source.Token); What baffles me about this is that there is no way from inside the method body to actually get at the token passed in (e.g., nothing like Task.CurrentTask.CancellationToken ). The token has to be provided through some other mechanism, such as the state object or captured in a lambda. So what purpose does providing the cancellation token in the constructor serve? Max Galkin Passing this token

Async network operations never finish

跟風遠走 提交于 2019-11-26 06:08:42
问题 I have several asynchronous network operations that return a task that may never finish: UdpClient.ReceiveAsync doesn\'t accept a CancellationToken TcpClient.GetStream returns a NetworkStream that doesn\'t respect the CancellationToken on Stream.ReadAsync (checking for cancellation only at the start of the operation) Both wait for a message that may never come (because of packet loss or no response for example). That means I have phantom tasks that never complete, continuations that will

Cancellation token in Task constructor: why?

徘徊边缘 提交于 2019-11-26 02:05:42
问题 Certain System.Threading.Tasks.Task constructors take a CancellationToken as a parameter: CancellationTokenSource source = new CancellationTokenSource(); Task t = new Task (/* method */, source.Token); What baffles me about this is that there is no way from inside the method body to actually get at the token passed in (e.g., nothing like Task.CurrentTask.CancellationToken ). The token has to be provided through some other mechanism, such as the state object or captured in a lambda. So what