Hi,
I have Console C# application that needs to write to a Google spreadsheet file every hour. I know that Task Scheduler is one of the solutions, but when the computer is shut down, it doesn't work. Does anyone know another solution how to run the application even though the computer is turned off? Is there any option to deploy the application to the server?

Amit MohantyRank #112 · 20K reputationPosted Jun 1, 2023, 6:24 AM EDT
If you want your C# application to run on a schedule even when the computer is turned off, you can consider deploying the application to a server or using a cloud-based solution. Cloud platforms like Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP) offer services for hosting and running applications. You can deploy your application to a cloud server or use serverless computing options like AWS Lambda or Azure Functions. These platforms often provide built-in scheduling capabilities that allow you to trigger your application periodically.
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 17, 2024, 2:30 PM EDT
Task Scheduler won't run your console application if the computer is shut down. Upload your C# application and configure the server environment (usually through a web interface or command line tools) to schedule its execution. Most cloud providers offer built-in scheduling functionalities or allow integration with third-party schedulers like Cron
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 17, 2024, 2:31 PM EDT
Here are example code snippets for the two approaches mentioned previously to run your C# console application every hour:
1. Deploying to a Cloud Server (using Azure App Service as an example):
C# Console Application: (This is just a sample; modify it to write to your Google spreadsheet)
using System; using Google.Apis.Sheets.v4; using Google.Apis.Sheets.v4.Data; public class SpreadsheetWriter { public static void WriteToSpreadsheet(string spreadsheetId, string range, string value) { // Replace these with your Google API credentials string clientId = "YOUR_CLIENT_ID"; string clientSecret = "YOUR_CLIENT_SECRET"; // ... Code to authenticate with Google Sheets API (omitted for brevity) var service = new SheetsService(new BaseClientService.Initializer() { HttpClientInitializer = new CredentialManager().GetCredentials(new ClientSecrets() { ClientId = clientId, ClientSecret = clientSecret }) }); var valueRange = new ValueRange(); valueRange.Values = new List>() { new List() { value } }; var updateRequest = service.Spreadsheets().Values.Update(spreadsheetId, range, valueRange); updateRequest.ValueInputOption = ValueInputOption.USER_ENTERED.ToString(); updateRequest.Execute(); Console.WriteLine($"Successfully wrote '{value}' to spreadsheet range '{range}'."); } public static void Main(string[] args) { string spreadsheetId = "YOUR_SPREADSHEET_ID"; // Replace with your spreadsheet ID string range = "Sheet1!A1"; // Replace with desired range in your spreadsheet string value = DateTime.Now.ToString(); // Example value (modify to your needs) WriteToSpreadsheet(spreadsheetId, range, value); } }WriteToSpreadsheetmethod every hour. Azure App Service provides built-in scheduling functionalities using WebJobs or Azure Functions. You can refer to Azure documentation for detailed instructions: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timerSam HobbsRank #107 · 20.5K reputationPosted Mar 17, 2024, 10:25 PM EDT
You could use a VM. That is a type of cloud. There are Windows VMs but Linux VMs are very abundant and you can use Visual Studio to develop a C# console application that can execute in Linux.