C
C#13mo 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
sibber13mo ago
first of all await waits for the task to finish so that timeout isnt acting as a timeout
cap5lut
cap5lut13mo 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
sibber13mo ago
preferably use a cancellation token and pass it around and dont have SendMessageBlaBla call RecieveBlaBla
cap5lut
cap5lut13mo ago
also please use $codeblocks to provide code, screenshots are annoying
sibber
sibber13mo ago
when reading that code i had no idea _chatrooms was being mutated
cap5lut
cap5lut13mo ago
$code
MODiX
MODiX13mo 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
Ang61913mo 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
sibber13mo ago
but the first isnt valid syntax where was that anyway
Ang619
Ang61913mo 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
sibber13mo 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
Ang61913mo 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
More Posts