locked
How to Create Async method that return value before sub method complete. RRS feed

  • Question

  • User-1423656093 posted

    Hi All,

    How to Create Async method that return value before sub method complete.

    private void Demo()
    {
        // Do something, given that the result doesn't matter.
    }
    
    public string Do()
    {
      Demo();
    
        // The following line will be executed without waiting for the result.
      var y = DoSomethingElse();
      return y;
    }

    In the above example, Do() method should not wait for Demo() Complete. if DoSomethingElse() completed, then it should return a value. The Demo method has been completed later.

    Thanks in advance.

    Monday, February 8, 2016 10:51 AM

Answers

  • User36583972 posted

    Hi Krish Kannaa,

    I have make a sample on my side. You can try the following code.

            static  void Main(string[] args)
            {
                Do();
                string result = DoSomethingElse();//The following line will be executed without waiting for the result
                Console.WriteLine(result);
                Console.ReadLine();
            }
    
            private static async Task<string> Do()
            {
                int t= await Task.Run(() => Demo());
                Console.WriteLine(t);
                return "";
            }
    
            public static int Demo()
            {
                // Do something, given that the result doesn't matter.
                int size = 0;
                for (int z = 0; z < 100; z++)
                {
                    for (int i = 0; i < 100; i++)
                    {
                        string value = i.ToString();
                        if (value == null)
                        {
                            return 0;
                        }
                        size += value.Length;
                    }
                }
                return size;
            }
    
            public static string DoSomethingElse()
            {
                //
                return "001";
            }

    More information baout Asynchronous Programming with Async and Await:

    https://msdn.microsoft.com/en-us/library/hh191443.aspx

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 11, 2016 7:46 AM

All replies

  • User1779161005 posted

    public Task<string> Foo()
    {
         return Task.FromResult("done");
    }

    Monday, February 8, 2016 2:11 PM
  • User36583972 posted

    Hi Krish Kannaa,

    I have make a sample on my side. You can try the following code.

            static  void Main(string[] args)
            {
                Do();
                string result = DoSomethingElse();//The following line will be executed without waiting for the result
                Console.WriteLine(result);
                Console.ReadLine();
            }
    
            private static async Task<string> Do()
            {
                int t= await Task.Run(() => Demo());
                Console.WriteLine(t);
                return "";
            }
    
            public static int Demo()
            {
                // Do something, given that the result doesn't matter.
                int size = 0;
                for (int z = 0; z < 100; z++)
                {
                    for (int i = 0; i < 100; i++)
                    {
                        string value = i.ToString();
                        if (value == null)
                        {
                            return 0;
                        }
                        size += value.Length;
                    }
                }
                return size;
            }
    
            public static string DoSomethingElse()
            {
                //
                return "001";
            }

    More information baout Asynchronous Programming with Async and Await:

    https://msdn.microsoft.com/en-us/library/hh191443.aspx

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 11, 2016 7:46 AM