如何在 c# 中的静态 类 中创建一个简单的回调
How do I create a simple callback in static classes in c#
我有一个静态 class,它有一个方法再次调用另外两个静态 class 方法
基本上在第一个静态 class 我想知道一旦操作在其他两个静态 classes
中完成
public static class FirstClass{
public static async System.Threading.Tasks.Task FirstClassMethod()
{
SecondClass. SecondClassMethod();
ThirdClass. ThirdClassMethod();
}
}
public static class SecondClass{
public static async System.Threading.Tasks.Task SecondClassMethod()
{
}
}
public static class ThirdClass{
public static async System.Threading.Tasks.Task ThirdClassMethod()
{
}
}
如果能帮助解决我的问题,我将不胜感激
使用Task.WhenAll
,您可以创建一个包装多个任务的单个任务,并在所有包装任务完成时完成。
public static class FirstClass{
public static async System.Threading.Tasks.Task FirstClassMethod()
{
return await Task.WhenAll(
SecondClass.SecondClassMethod(),
ThirdClass.ThirdClassMethod()
);
}
}
public static class SecondClass{
public static async System.Threading.Tasks.Task SecondClassMethod()
{
}
}
public static class ThirdClass{
public static async System.Threading.Tasks.Task ThirdClassMethod()
{
}
}
使用 await
关键字等待 Task
完成。
using System.Threading.Tasks;
public static async Task FirstClassMethod()
{
await SecondClass. SecondClassMethod();
await ThirdClass. ThirdClassMethod();
...
}
顺便说一句:
You should consider not using await only if you're sure that you don't want to wait for the asynchronous call to complete and that the called method won't raise any exceptions.
我有一个静态 class,它有一个方法再次调用另外两个静态 class 方法
基本上在第一个静态 class 我想知道一旦操作在其他两个静态 classes
中完成 public static class FirstClass{
public static async System.Threading.Tasks.Task FirstClassMethod()
{
SecondClass. SecondClassMethod();
ThirdClass. ThirdClassMethod();
}
}
public static class SecondClass{
public static async System.Threading.Tasks.Task SecondClassMethod()
{
}
}
public static class ThirdClass{
public static async System.Threading.Tasks.Task ThirdClassMethod()
{
}
}
如果能帮助解决我的问题,我将不胜感激
使用Task.WhenAll
,您可以创建一个包装多个任务的单个任务,并在所有包装任务完成时完成。
public static class FirstClass{
public static async System.Threading.Tasks.Task FirstClassMethod()
{
return await Task.WhenAll(
SecondClass.SecondClassMethod(),
ThirdClass.ThirdClassMethod()
);
}
}
public static class SecondClass{
public static async System.Threading.Tasks.Task SecondClassMethod()
{
}
}
public static class ThirdClass{
public static async System.Threading.Tasks.Task ThirdClassMethod()
{
}
}
使用 await
关键字等待 Task
完成。
using System.Threading.Tasks;
public static async Task FirstClassMethod()
{
await SecondClass. SecondClassMethod();
await ThirdClass. ThirdClassMethod();
...
}
顺便说一句:
You should consider not using await only if you're sure that you don't want to wait for the asynchronous call to complete and that the called method won't raise any exceptions.