1

I have a Console Application that every x minutes makes an API call and, based off the response, control some IoT switches.
I need this to run 24/24 on a windows machine. I figured out that running it as a console application the risk is that the timer could broke over time.
How can I convert this app to a windows service without rewriting it from zero? I also implemented some console-logging stuff, so I'd like to run the application as console, and have a service to generate the timer elapsed events, if it's possible.

Ps: The timer interval is something between 2 to 5 minutes, and the program run only during day (it checks the photovoltaic production and the API request limit is 300 per day, so I check only when sun is actually out)

leolamarra
  • 35
  • 1
  • 6
  • Please have a look here: https://csharp.christiannagel.com/2019/10/15/windowsservice/ – Markus Meyer Sep 25 '22 at 15:54
  • @MarkusMeyer The problem is that I already have my program, and I would really like to avoid rewriting it as a native worker or a service – leolamarra Sep 25 '22 at 16:14
  • Microsoft published a more old-school service tutorial [here](https://learn.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer). – Axel Kemper Sep 25 '22 at 16:14
  • Keep it as simple console application, run it as a [scheduled task](https://learn.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-start-page). – Christian.K Sep 26 '22 at 04:32

1 Answers1

1

If the application is well-written, then the application logic is separated from the user-interface (CLI in this case) and you could simply implement only the UI in your Windows app and reuse the same classes that you were using in the initial project.

If the prerequisite above is not met, that is, the application logic is mixed up with the user interface handler, then you will need to refactor the initial project so you will end up with a project where the app logic is separated from the user interface handler.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • The application logic is completely separated from the UI, so I could simply use my classes in a new project, but I was searching for a way to have a windows service with a console output – leolamarra Sep 25 '22 at 16:59
  • @CoderLel I see. Maybe this source can help you https://stackoverflow.com/questions/4362111/how-do-i-show-a-console-output-window-in-a-forms-application – Lajos Arpad Sep 25 '22 at 17:07
  • Problem is windows service cannot interact with user interface, so you can't really show a console with a service. A possible workaround is instead of using console output, use a textfile output. Like a `.log`. [Reference](https://stackoverflow.com/questions/73845871/c-sharp-service-from-console-application) if you want to know why. – Xiang Wei Huang Sep 26 '22 at 02:58