skip to Main Content
namespace WebReader
{
    public partial class frm_Main : Form
    {
        public frm_Main()
        {
            InitializeComponent();
        }

        private void frm_Main_Load(object sender, EventArgs e)
        {
            using var clt= new HttpClient();
            var res = clt.GetAsync("https://daera.net/dmoon/testfile.txt");

            while (true)
            {
                if (res.IsCompletedSuccessfully)
                {
                    var msg = clt.GetStringAsync("https://daera.net/dmoon/testfile.txt");
                    MessageBox.Show(msg.ToString());
                    break;
                }
            }
        }
    }
}

This code is supposed to show the contents of the text file https://daera.net/dmoon/testfile.txt, but it shows a message box with some technical information regarding clt object and nothing else.

What am I doing wrong? I am using Visual Studio 2019.

2

Answers


  1. Oversimplifying, but you want to await the results of Async methods:

    using var clt= new HttpClient();
    var res = await clt.GetAsync("https://daera.net/dmoon/testfile.txt");
    

    Do this, and you won’t need the while (true) loop.

    Login or Signup to reply.
  2. As I said in the comments, the GetStringAsync() call is also asynchronous and returns a Task<String>.

    You have several options for synchronizing these tasks with your main program.

    1. The one you used: a while(true) loop checking for IsCompletedSuccessfully. This is not recommended at all, because it eats your resources doing nothing.
    2. Using Task.Wait(). This is a blocking call and what you’re looking for. (Edited: it’s closer to your original loop without eating resources; but you probably don’t want to block your form until the HTTP call ends, which would make your program unresponsive. Look for option 3 or Task.ContinueWith())
    3. Using await as in @Joel’s answer (*).

    This is your code, using option 1 for GetAsync() (again, not recommended) and option 2 for GetStringAsync().

    namespace WebReader
    {
        public partial class Program
        {
            static void Main ()
            {
                using var clt= new HttpClient();
                var res = clt.GetAsync("https://daera.net/dmoon/testfile.txt");
                while (true)
                {
                    if (res.IsCompletedSuccessfully)
                    {
                        var responseTask = clt.GetStringAsync("https://daera.net/dmoon/testfile.txt");
                        responseTask.Wait();
                        Console.WriteLine(responseTask.Result.ToString());
                        break;
                    }
                    
                }
            }
        }
    }
    

    (*) When you use await before an asynchronous function call, the current function (ie. the caller) won’t execute the instructions following the call until the asynchronous task completes; but it will immediately return to its own caller. That is, the caller will also be asynchronous (and that’s the reason for the mandatory async keyword).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search