mkvakin
mkvakin

Reputation: 631

Can a C# Azure function create another thread?

Inside an Azure function I have to start a new thread and wait for its results by checking a status variable in a While loop. Once the thread completed its execution, the function will proceed to the end.

Is it a legitimate way of programming Azure functions? What will happen to my function app if While loop doesn't break or a thread doesn't return?

Upvotes: 0

Views: 1124

Answers (1)

misha130
misha130

Reputation: 5726

Yes, you can but you should use Durable Functions for this which are a set of premade libraries that were made specifically for such cases.

There are quite a few patterns that range from human intervention, orchestration and such.

Your code might look something like this as per their examples:

[FunctionName("BudgetApproval")]
public static async Task Run(
    [OrchestrationTrigger] IDurableOrchestrationContext context)
{
    bool approved = await context.WaitForExternalEvent<bool>("Approval");
    if (approved)
    {
        // approval granted - do the approved action
    }
    else
    {
        // approval denied - send a notification
    }
}

Or another option is you make a durable entity function that holds the state and then an event when that changes. Or a monitor function which basically loops around to see if state has changed. There are plenty of options

Upvotes: 1

Related Questions