admin管理员组

文章数量:1122846

I have a console application written in C# that I plan to deploy to Azure. I need to be able to programmatically control its execution — specifically, start and stop the app using code (c#). Any suggestion?

Since I am new to deploying console applications in Azure, I don’t have any prior knowledge or experience.

I have a console application written in C# that I plan to deploy to Azure. I need to be able to programmatically control its execution — specifically, start and stop the app using code (c#). Any suggestion?

Since I am new to deploying console applications in Azure, I don’t have any prior knowledge or experience.

Share Improve this question edited Nov 21, 2024 at 14:55 DarkBee 15.8k8 gold badges69 silver badges110 bronze badges asked Nov 21, 2024 at 14:48 sarasara 11 bronze badge 3
  • 2 How are you planning to "deploy" it? What is going to be hosted in? – Fildor Commented Nov 21, 2024 at 14:57
  • 1 Do you really want to start/stop the entire app or just control if it is performing a specific action? – DavidG Commented Nov 21, 2024 at 14:59
  • Why not export the functionality of the CLI as a DLL instead? – Valerij Dobler Commented Nov 21, 2024 at 15:14
Add a comment  | 

2 Answers 2

Reset to default -1

Does your application support CTRL+C to stop it? That's how Azure will call your app to stop it. If you use a background task, or otherwise cancellable tasks, then it should exit cleanly.

For example:

static async Task Main()
{
    await Host.CreateDefaultBuilder().RunConsoleAsync();
}

Will automatically support CTRL+C to exit.

You can use Process.start to call a program. See below to Kill a process.

System.Diagnostics.Process.Start().
Process.Start("notepad.exe", fileName);

foreach (var process in Process.GetProcessesByName("whatever"))
{
    process.Kill();
}

本文标签: cHow to programmatically stop and start a console applicationStack Overflow