C
C#2y ago
Ang619

I never reach the return, and i dont know why?

I have this Task: (Picture 1), which also calls two other Tasks (Picture 2 and Picture 3). However i never reach the return of the first Task, and i dont understand why? The Program breaks out of the while(true) loop in Picture 3, and then the original Task (Picture 1) is not continuing.
12 Replies
sibber
sibber2y ago
first of all await waits for the task to finish so that timeout isnt acting as a timeout
cap5lut
cap5lut2y ago
because u await that ReceiveDiscoveryResponseAsync() finishes and only some time afterwards close the socket u basically have something like
bool run = true;
while (true) {
if (!run) {
break;
}
}
run = false;
bool run = true;
while (true) {
if (!run) {
break;
}
}
run = false;
sibber
sibber2y ago
preferably use a cancellation token and pass it around and dont have SendMessageBlaBla call RecieveBlaBla
cap5lut
cap5lut2y ago
also please use $codeblocks to provide code, screenshots are annoying
sibber
sibber2y ago
when reading that code i had no idea _chatrooms was being mutated
cap5lut
cap5lut2y ago
$code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Ang619
Ang619OP2y ago
Okay thanks for the responses. I will further think about it and try your solutions That helped a lot. I changed await chatroomList = await discovery.StartDiscoveryAsync(); into chatroomList = await discovery.StartDiscoveryAsync(); That did the trick. Thank you really much.
sibber
sibber2y ago
but the first isnt valid syntax where was that anyway
Ang619
Ang619OP2y ago
oh its when i call the task on my application.
var discovery = new ChatroomsDiscovery();
chatroomList = await discovery.StartDiscoveryAsync();
var discovery = new ChatroomsDiscovery();
chatroomList = await discovery.StartDiscoveryAsync();
But when i think about it, the delay really doesnt do anything. Need to think about it for a sec.
sibber
sibber2y ago
yes thats what im saying you probably want something like this:
Task sendDiscoverMessage = SendDiscoverabilityMessageAsync();
if (sendDiscoverMessage != await Task.WhenAny(sendDiscoverMessage, Task.Delay(_discoveryTimeoutMS)))
{
// handle timeout (should probably cancel sendDiscoverMessage)
}
Task sendDiscoverMessage = SendDiscoverabilityMessageAsync();
if (sendDiscoverMessage != await Task.WhenAny(sendDiscoverMessage, Task.Delay(_discoveryTimeoutMS)))
{
// handle timeout (should probably cancel sendDiscoverMessage)
}
Ang619
Ang619OP2y ago
I will take a good look at this in the evening. Looks really good on the first look
Want results from more Discord servers?
Add your server