Home>

I'm a beginner.
I want to execute the code under a different questioner, but I don't know how to write the call.

public static async Task<string>code ()
{
string url = "https://login.microsoftonline.com/common/oauth2/token";
               var nvc = new List<KeyValuePair<string, string >>();
               nvc.Add (new KeyValuePair<string, string>("client_id", "3181129a-*******"));
               nvc.Add (new KeyValuePair<string, string>("scope", "https://graph.microsoft.com/.default"));
               nvc.Add (new KeyValuePair<string, string>("client_secret", "******************"));
               nvc.Add (new KeyValuePair<string, string>("grant_type", "client_credentials"));
               var client = new HttpClient ();
               var req = new HttpRequestMessage (HttpMethod.Post, url) {Content = new FormUrlEncodedContent (nvc)};
               var res = await client.SendAsync (req);
var result = awit res.Content.ReadAsStringAsync ();
return res.ToString ();
}

I tried code ();etc., but the subsequent processing does not proceed.

Thank you.

It is an addition.

The quotation is as follows.
https://www.tutorialfor.com/go.php?id=97928

If i have any shortages, please leave a comment.

It is an addition.

It doesn't go back to what you expected, rather than the process not progressing.

public static void Main (string [] args)
       {
           Create ();
           Createsite ();

       }
       public static async void Create ()
       {
           await code ();
           Console.WriteLine ("ABC");
}


I want to go back to Console.WriteLine ("ABC");above, but it goes back to main.

c#
  • Answer # 1

    As I wrote in the comment of the question, since C # 7.1 or later, you can also add async to Main, so it is recommended to update the environment and add async/await from top to bottom (all asynchronous). ..

    For example, the following code

    using System;
    using System.Threading.Tasks;
    namespace ConsoleAppAsync4
    {
        class Program
        {
            static async Task Main (string [] args)
            {
                Console.WriteLine ("Before calling Create");
                await Create ();
                Console.WriteLine ("Create wait end");
            }
            static async Task Create ()
            {
                Console.WriteLine ("Before calling Code");
                await Code ();
                Console.WriteLine ("Code wait end");
            }
            static async Task Code ()
            {
                Console.WriteLine ("Starting Task.Delay (3000)");
                await Task.Delay (3000);
                Console.WriteLine ("Task.Delay (3000) End");
            }
        }
    }

    The execution result is as follows.

    Before calling Create
    Before calling Code
    The beginning of Task.Delay (3000)
    Task.Delay (3000) End
    Code Waiting end
    Create Waiting end

    The code in question is that there is no await when calling Create in the Main method above, but then the execution result looks like this:

    Before calling Create
    Before calling Code
    The beginning of Task.Delay (3000)
    Create Waiting end

    The Code method Task.Delay (3000);is executed in another thread, but since there is no await in the Create method, it does not wait for the completion of execution in another thread, and immediately Console.WriteLine ("Create wait end" It means that ");has been executed and Main has ended.

  • Answer # 2

    >It doesn't return to what I expected, rather than the processing not progressing.

    If you insert the log as below,
    The log is output as shown below, and it looks like it is working as expected.
    Why is the Create end displayed before the code end? Is it the question?

    1. 1. Create start
    2. 2. code start
    3. 3. Create finished
    4. code end

          public async void Create ()
            {
                Console.WriteLine ("code start");
                await code ();
                Console.WriteLine ("code end");
            }
            private void button1_Click (object sender, EventArgs e)
            {
                Console.WriteLine ("Create Start");
                Create ();
                Console.WriteLine ("Create finished");
            }