什么是C#异步方法,是否返回父方法? [英] What is C# asynchronous method , does it return to parent method ?

查看:56
本文介绍了什么是C#异步方法,是否返回父方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



在异步编程中如果方法(子方法)在方法内调用异步形式(父方法),

然后它并行运行。我无法理解一件事 -



1.父方法是否有任何子方法的痕迹,所以Parent方法可以知道Child方法执行有已完成。



2.如果父方法执行将在子方法执行之前完成,那么Child方法会发生什么。儿童方法会显示输出吗。



我尝试过:



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

{

公共静态类程序

{

//作为Main的父方法()

public static void Main()

{

ThreadPool.QueueUserWorkItem(ChildMethod,5);

Console.WriteLine(Hi我在父方法。);

// Console.ReadLine();

}



//这个方法的ChildMethod

私有静态void ChildMethod(对象状态)

{

//等待besacue parrent方法必须为孩子完成方法。

Thread.Sleep(1000);

Console.WriteLine(我喜欢Child Method。);

Console.ReadLine();

}

}

}

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();
}
}
}

推荐答案

Quote:

Parent方法是否有任何子方法的跟踪,因此Parent方法可以了解Child方法的执行情况完成。

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

如果使用第二个参数传递的状态对象不包含一些告诉正在发生的事情的方法,那么除非你注册一个等待句柄,否则调用任务不能告诉子任务发生了什么。 。 ThreadPool类(System.Threading) [< a href =https://msdn.microsoft.com/en-us/library/system.threading.threadpool(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ] - 请参阅备注部分。

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.

引用:

如果父项将会发生什么方法执行将在子方法执行之前完成。子方法是否会显示输出。

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.



这是一个困难的方法:它取决于哪个线程启动子进程。如果它是主线程 - 从你的示例中的 main 方法执行 - 那么当该线程结束时,所有子任务也将如此,无论它们的状态如何所有内存和其他资源都将被回收到操作系统。

如果不是,则子线程将继续。


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.


这篇关于什么是C#异步方法,是否返回父方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆