0
private void Button1_Click(object sender, EventArgs e)
{
    string a = Get2().ConfigureAwait(false).GetAwaiter().GetResult();
    richTextBox1.AppendText(a);
}

public async Task<string>Get2()
{
    return await "https://raw.githubusercontent.com/SMAPPNYU/ProgrammerGroup/master/LargeDataSets/sample-tweet.raw.json".GetStringAsync().ConfigureAwait(false);
}

The first button click hangs for a moment then it works fine. why does the form freeze at the first time click the button?

Maximilian Ast
  • 3,369
  • 12
  • 36
  • 47

2 Answers2

2

That the reason that the freezes is because you use a async call synchronusly (see Is Task.Result the same as .GetAwaiter.GetResult()?) in the UI thread. Try using async/await there aswell (GetResult() will block the UI thread like .Result does):

private async void Button1_Click(object sender, EventArgs e)
{
    string a = await Get2().ConfigureAwait(true); // true needed to continue on the UI thread
    richTextBox1.AppendText(a);
}

public async Task<string> Get2()
{
    return await "https://raw.githubusercontent.com/SMAPPNYU/ProgrammerGroup/master/LargeDataSets/sample-tweet.raw.json".GetStringAsync().ConfigureAwait(false);
}
Maximilian Ast
  • 3,369
  • 12
  • 36
  • 47
  • 1
    The button click handler should return `void` not `Task`. Event handlers are the exception to the rule that `async` methods should return either `Task` or `Task` – Igor Sep 17 '19 at 21:13
  • could you test it out it freeze also the first click – schoko Bong Sep 17 '19 at 21:17
  • 1
    How do you define "freeze" ? Is the UI freezing so where you can't even move the window? Or is the value in the text box just not updating? How long is it freezing for? – Gabriel Luci Sep 17 '19 at 21:25
  • If the payload would be much bigger the ui could also freeze during the update `richTextBox1.AppendText(a);` but I don't think that would be noticeable with such a small payload – Maximilian Ast Sep 17 '19 at 21:26
  • @schokoBong on my side there is no freezing of the UI whatsoever, I assume you mean why is the value not updating instantly after the click. --> Gabriel Luci's answer – Maximilian Ast Sep 17 '19 at 22:21
  • no, if I click the button I cant do anything for 1-2 secounds then If I example spam the button It dont freeze – schoko Bong Sep 18 '19 at 11:00
1

Seems like you have defined GetStringAsync() as an extension method, which you havent shown here (or maybe it's part of a library you're using). But assuming it uses HttpClient in the background, then the first try works as you would expect, but leaves the TCP connection open. The second request uses the same connection, which saves the time it takes to set up the connection and do the SSL handshake.

That's just a wild guess though. I'd have to see the code for GetStringAsync() to give a better guess.

Side note: consider making Button1_Click async and using await. What you have right now will freeze the UI until the request returns. If you use await, it won't.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84