Skip to main content
  1. About
  2. For Teams
Asked
Modified 3 years ago
Viewed 1k times
0

im new to C# and i was trying to figure out async and await . for practice i was trying to start method 1 in which method 2 is called twice . method 2 takes a value and increases it by 1 each 200 ms . instead of running method 2 the program ends after the first line of method 1 .

    static void Main(string[] args)
    {
        Method1();
    }

    static int Method2(int x)
    {
        for (int i = 0; i < 10; i++)
        {
            x += 1;
            Console.WriteLine(x);
            Thread.Sleep(200);
        }
        Console.WriteLine("final" + " " + x + " " + Thread.CurrentThread.ManagedThreadId);
        return x;

    }
    static async Task Method1()
    {
        Console.WriteLine("1 running");
        int result1 = await Task.Run(() => Method2(0));
        int result2 = await Task.Run(() => Method2(result1));
        Thread.Sleep(1000);
        Console.WriteLine("result " + result2 * 2);
    }

what am i doing wrong here ?

5
  • What do you expect to happen? What is happening that is unexpected?
    Özgür Güzeldereli
    –  Özgür Güzeldereli
    2022-10-15 16:11:21 +00:00
    Commented Oct 15, 2022 at 16:11
  • ive tried it before with threading and tasks and its supposed to take the value 0 , add 1 ten times to it and then repeat it again . and at the end of method one double it so the final result shoud be 40 . instead the program just ends after the firs line of method 1 ( Consol.WriteLine("1 running")).
    mhtaaghi
    –  mhtaaghi
    2022-10-15 16:14:46 +00:00
    Commented Oct 15, 2022 at 16:14
  • 1
    First of all you need async Main here and await Method1(); inside it. Use it instead your current non-async Main
    Serg
    –  Serg
    2022-10-15 16:18:01 +00:00
    Commented Oct 15, 2022 at 16:18
  • 1
    Async != multithreading
    Daniel A. White
    –  Daniel A. White
    2022-10-15 16:18:36 +00:00
    Commented Oct 15, 2022 at 16:18
  • 2
    You aren’t telling the program to wait for the async tasks to finsih
    Daniel A. White
    –  Daniel A. White
    2022-10-15 16:19:13 +00:00
    Commented Oct 15, 2022 at 16:19

1 Answer 1

4

When calling Method() you aren't waiting on it. It returns a task object that is not acted upon, and then Main() dutifully returns, which ends the program.

You can do this in Main():

public static void Main() {
    Method1().GetAwaiter().GetResult();
}

Or use async Main() instead:

public static async Task Main() {
    await Method1();
}
Sign up to request clarification or add additional context in comments.

1 Comment

The second example is cleaner. static async Task Main() { await Method1(); }

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.