Click here to Skip to main content
15,904,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

In Asynchronous programming if a method (Child Method) call "Asynchronously"form inside the method (Parent Method),
Then it run parallel. i am not able to understand one thing -

1.Does Parent method have any trace of child method,So that Parent method can be aware about the Child method execution has been Finish.

2.What will be happen to Child Method if Parent Method Execution will finished before the Child Method execution. Does child method will show the output.

What I have tried:

Quote:
using System;
using System.Threading;
namespace Threads
{
public static class Program
{
//parent method as Main()
public static void Main()
{
ThreadPool.QueueUserWorkItem(ChildMethod, 5);
Console.WriteLine("Hi i am in Parent Method.");
// Console.ReadLine();
}

// This method's ChildMethod
private static void ChildMethod(Object state)
{
//wait besacue parrent method has to be finish befor child method.
Thread.Sleep(1000);
Console.WriteLine("Hi i am in Child Method.");
Console.ReadLine();
}
}
}
Posted
Updated 14-Mar-18 22:39pm
Comments
Richard Deeming 15-Mar-18 14:30pm    
Much simpler to use async methods:
Asynchronous programming | Microsoft Docs[^]

private static async Task ChildMethod()
{
    await Task.Delay(1000);
    Console.WriteLine("Hi I am in Child Method.");
    Console.ReadLine();
}

static void Main()
{
    Task child = ChildMethod();
    Console.WriteLine("Hi I am in Parent Method.");
    child.GetAwaiter().GetResult();
}


If you're using an up-to-date version of Visual Studio, you can even make the Main method async:
static async Task Main()
{
    Task child = ChildMethod();
    Console.WriteLine("Hi I am in Parent Method.");
    await child;
}

1 solution

Quote:
Does Parent method have any trace of child method,So that Parent method can be aware about the Child method execution has been Finish.
If the state object passed with the second argument does not include some means of telling what is going on, then unless you register a wait handle the "calling task" cannot tell what it happening with the "child task". ThreadPool Class (System.Threading)[^] - see teh "Remarks" section.
Quote:
What will be happen to Child Method if Parent Method Execution will finished before the Child Method execution. Does child method will show the output.

That's a difficult one: it depends on which thread initiates the child process. If it's the main thread - the one that executes from the main method as in your example - then when that thread ends, so will all the child tasks, regardless of their status and all memory and other resources will be recycled back to the OS.
If it isn't, then the child thread will continue.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900