调用 Sub Orchestrator 时不要等待 - Azure Durable Functions

Don't wait when calling Sub Orchestrator - Azure Durable Functions

假设我在一个 Durable Function 中,我希望像这样调用一个 suborchestrator 函数:

context.CallSubOrchestratorAsync("FunctionName","InstanceId",Object);

现在,即使我不在调用前放置 await,持久函数也会等到子协调器完成。我有什么办法可以避免这种情况吗?我只想调用函数而忘记它。

你不能完全忘记任务,但是你不需要等到它们再继续,例如

public class TaskRunner
{
    private List<IAsyncAction> taskList;
    private AsyncActionCompletedHandler handleThreadComplete;
    public TaskRunner()
    {
        taskList = new List<IAsyncAction>();
        handleThreadComplete = new AsyncActionCompletedHandler(HandleThread);
    }

    public void RunTasks()
    {
        IAsyncAction t1 = Task.Run(() => LongTask(3000)).AsAsyncAction();
        t1.Completed = handleThreadComplete;
        taskList.Add(t1);
        Console.WriteLine("Started Task 1");
        IAsyncAction t2 = Task.Run(() => LongTask(2000)).AsAsyncAction();
        t2.Completed = handleThreadComplete;
        taskList.Add(t2);
        Console.WriteLine("Started Task 2");

        // Start a task and forget about it
        Task.Run(() => LongTask(1000)).ConfigureAwait(false);
        Console.WriteLine("Started Task 3 - Fire and Forget");
    }

    public int TaskCount
    {
        get
        {
            return taskList.Count;
        }
    }
    private void HandleThread(IAsyncAction asyncInfo, AsyncStatus asyncStatus)
    {
        taskList.Remove(asyncInfo);
        Console.WriteLine("Task Complete");
    }
    private async Task LongTask(int waitFor)
    {
        Thread.Sleep(waitFor);
        Console.WriteLine("Hello after " + waitFor);
    }
}

public class Program
{
    public static async Task Main(string[] args)
    {
        TaskRunner tr = new TaskRunner();
        tr.RunTasks();
        while(tr.TaskCount > 0)
        {
            Console.WriteLine("Running " + tr.TaskCount + " tasks");
            Thread.Sleep(100);
        }
        Console.WriteLine("Tasks Complete");

    }
}

这将允许您继续前进,但是您需要保留对任务的引用,否则它们将被终止。