C
C#•4d ago
Ryze

Problem with the first iteration in the loop, wont wait the set delay

I'm basically trying to iterate through users and then add a delay for each iteration, delay is user set. There is however a base delay that is also user set, I just calculate the final delay with base delay + delay. I use a variable to calculate which user its at, if its the first then its 0, second 1, third 2 etc. It resets after all the users have been done sending. Problem is, the first user sends instantly after 1 second, ignoring the delay.
13 Replies
Ryze
RyzeOP•4d ago
c#
if (Tokens.Count > 1)
{
Console.WriteLine("It seems as if you have more than 1 token stored, would you like to have a message delay on each token?");
Console.Write("Y/N: ");
char answer = char.ToUpper(Console.ReadKey().KeyChar);
Console.WriteLine();

if (answer == 'Y')
{
int index = 0;
Console.Write("What delay would you like between each token? Write in seconds: ");
int tokenDelay = Convert.ToInt32(Console.ReadLine());
while (true)
{

if (backgroundWorker.CancellationPending)
{
break;
}

foreach (string token in Tokens)
{

await Task.Delay(100);
int finalDelay = delay;
c#
if (Tokens.Count > 1)
{
Console.WriteLine("It seems as if you have more than 1 token stored, would you like to have a message delay on each token?");
Console.Write("Y/N: ");
char answer = char.ToUpper(Console.ReadKey().KeyChar);
Console.WriteLine();

if (answer == 'Y')
{
int index = 0;
Console.Write("What delay would you like between each token? Write in seconds: ");
int tokenDelay = Convert.ToInt32(Console.ReadLine());
while (true)
{

if (backgroundWorker.CancellationPending)
{
break;
}

foreach (string token in Tokens)
{

await Task.Delay(100);
int finalDelay = delay;
c#
if (index != 0)
{
Console.WriteLine("Hello");
finalDelay += tokenDelay * index;
}

Console.WriteLine($"Index: {index}, Delay: {finalDelay}");

using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", token);

HttpResponseMessage infoResponse = await client.GetAsync("https://discord.com/api/v10/users/@me");

string url = $"https://discord.com/api/v10/channels/{channelID}/messages";

var content = new StringContent($"{{\"content\": \"{message}\"}}", Encoding.UTF8, "application/json");

var response = await client.PostAsync(url, content);

if (response.IsSuccessStatusCode)
{
if (infoResponse.IsSuccessStatusCode)
{
string userInfo = await infoResponse.Content.ReadAsStringAsync();
dynamic information = JsonConvert.DeserializeObject(userInfo);

username = information.username;

Console.WriteLine($"Message sent successfully from {username}");
}
}
else
{
Console.WriteLine($"Failed to send message: {response.StatusCode}");
}


Console.WriteLine("reached task.delay");
await Task.Delay(finalDelay * 1000);
}
index++;
}
index = 0;
}

}
c#
if (index != 0)
{
Console.WriteLine("Hello");
finalDelay += tokenDelay * index;
}

Console.WriteLine($"Index: {index}, Delay: {finalDelay}");

using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", token);

HttpResponseMessage infoResponse = await client.GetAsync("https://discord.com/api/v10/users/@me");

string url = $"https://discord.com/api/v10/channels/{channelID}/messages";

var content = new StringContent($"{{\"content\": \"{message}\"}}", Encoding.UTF8, "application/json");

var response = await client.PostAsync(url, content);

if (response.IsSuccessStatusCode)
{
if (infoResponse.IsSuccessStatusCode)
{
string userInfo = await infoResponse.Content.ReadAsStringAsync();
dynamic information = JsonConvert.DeserializeObject(userInfo);

username = information.username;

Console.WriteLine($"Message sent successfully from {username}");
}
}
else
{
Console.WriteLine($"Failed to send message: {response.StatusCode}");
}


Console.WriteLine("reached task.delay");
await Task.Delay(finalDelay * 1000);
}
index++;
}
index = 0;
}

}
this is for learning purposes and I do not condone using user tokens and requesting HTTP via discord api I am a beginnering and I am trying to learn by doing. This will not be used for discord but rather for educational purposes only. What I don't understand is why the heck the first iteration wont wait the set delay this is what debugging tells me: Index: 0, Delay: 3 Message sent successfully from ryze07 reached task.delay It tells me the correct base delay But it instantly sends
ero
ero•4d ago
you just do await Task.Delay(100);? is that supposed to be await Task.Delay(delay);? or tokenDelay, or finalDelay
this_is_pain
this_is_pain•4d ago
moving some code away in other methods would help outline the logic or even just removing some indentation layer
Ryze
RyzeOP•4d ago
Thats just a small delay before the iteration happend, thats supposed to be like that. Incase something happens and it needs that, really I just put it there to see if it would work Ill do that sorry await Task.Delay(finalDelay * 1000) is the main wait, *1000 is so it is counted in seconds.
this_is_pain
this_is_pain•4d ago
ok but do you agree that Delay(finalDelay * 1000) happens after http call has been made
Ryze
RyzeOP•4d ago
oh dude im so stupid thank you so much idk how i didnt notice that
Angius
Angius•4d ago
dynamic 🤮
this_is_pain
this_is_pain•4d ago
what? is there a dynamic and i didn't see it? edit: the deserialization... also Convert.ToInt32 should be TryParse
Ryze
RyzeOP•4d ago
Will try thanks Isnt dynamic a faster option ?
Angius
Angius•4d ago
No It's the least performant way you could've possibly chosen
leowest
leowest•4d ago
and painful
Ryze
RyzeOP•4d ago
Oh, how uneducated i am
Angius
Angius•4d ago
Also, could use the built-in JSON serialization instead of Newtonsoft, maybe Depending on the context this code is used in

Did you find this page helpful?