异步功能执行完毕后执行C#执行功能

时间:2018-01-11 11:30:09

标签: c# asynchronous

我必须执行批处理。生成ID并将其存储在列表中。我需要在生成所有ID之后执行该函数。问题是这些Ids是在异步函数中生成的。由于我使用的是第三方API,因此必须将此函数标记为异步。这是代码:

static void SaveFiles()
{
    try
    {
            foreach (FileInfo file in files)
            {
                //The following is async function.
                MakeAnalysisRequest(file.FullName, file.Name);
            }

            //The following needs to be called only after the MakeAnalysisRequest function has populated "Ids"
            if (Ids.Count > 1)
                CallBatchProcess(Ids);

            Console.WriteLine("Processing images...");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("There was an error! ");
    }
    Console.ReadLine();
}

static async void MakeAnalysisRequest(string imageFilePath, string fileName)
{
    //Here a list is populated.
    //List<string> Ids = new List<string();
    Ids.Add(obj.Id);
}

3 个答案:

答案 0 :(得分:0)

根据我的理解,您不需要异步执行该方法。您只需要调用异步方法。如果是这种情况,您可以简单地在 MakeAnalysisRequest 方法的异步调用上使用Wait(),或者甚至更好地使用您正在调用的api方法。



    static void SaveFiles()
    {
        try
        {
            foreach (FileInfo file in files)
            {
                //The following is async function.
                MakeAnalysisRequest(file.FullName, file.Name).Wait(); // The only change
            }

            //The following needs to be called only after the MakeAnalysisRequest function has populated "Ids"
            if (Ids.Count > 1)
                CallBatchProcess(Ids);

            Console.WriteLine("Processing images...");
        }
        catch (Exception ex)
        {
            Console.WriteLine("There was an error! ");
        }
        Console.ReadLine();
    }

    static void async MakeAnalysisRequest(string imageFilePath, string fileName)
    {
        //Here a list is populated.
        //List Ids = new List();
        Ids.Add(obj.Id);
    }

重要的是要注意这将使调用同步(它将等待异步调用完成)。如果您打算异步执行它,则需要更改应用程序的实现方式,以便在执行完成操作之前保持其执行。

答案 1 :(得分:0)

将MakeAnalysisRequest设为任务。并用多线程打电话给你,如

tasks.Add(Task.Factory.StartNew(() => MakeAnalysisRequest(fullName, name)));

等待所有线程完成,您可以使用其他代码进行处理。

代码示例:

static void SaveFiles(){
    List<Tasks> tasks = new List<Task>();
        try
        {
                foreach (FileInfo file in files)
                {
                    //The following is async function.
                    string fullName=file.FullName;
                    string name=file.Name;
                    tasks.Add(Task.Factory.StartNew(() => MakeAnalysisRequest(fullName, name)));
                }
                Task.WaitAll(tasks.ToArray());
                //The following needs to be called only after the MakeAnalysisRequest function has populated "Ids"
                if (Ids.Count > 1)
                    CallBatchProcess(Ids);

                Console.WriteLine("Processing images...");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("There was an error! ");
        }
        Console.ReadLine();
    }

    static async Task MakeAnalysisRequest(string imageFilePath, string fileName)
    {
        //Here a list is populated.
        //List<string> Ids = new List<string();
        Ids.Add(obj.Id);
    }

答案 2 :(得分:-1)

如果异步await中没有MakeAnalysisRequest,它将同步执行。如果是这种情况,所有调用都将结束,毕竟您的方法将被执行。

但如果MakeAnalysisRequestawait语句,使其成为异步,您可以创建一个Task数组并等待它们。

首先更改您的方法以返回Task

static async Task MakeAnalysisRequest(string imageFilePath, string fileName)
{
    //Here a list is populated.
    // Some await statement
    //List<string> Ids = new List<string();
    Ids.Add(obj.Id);
}

static void SaveFiles()
{
    try
    {
            var tasks = new List<Task>();

            foreach (FileInfo file in files)
            {
                //The following is async function.
                tasks.Add(MakeAnalysisRequest(file.FullName, file.Name));
            }

            Task.WaitAll(tasks.ToArray());

            //The following needs to be called only after the MakeAnalysisRequest function has populated "Ids"
            if (Ids.Count > 1)
                CallBatchProcess(Ids);

            Console.WriteLine("Processing images...");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("There was an error! ");
    }
    Console.ReadLine();
}