title: Run Python Windows service when not logged in
body: <p>My Flask service stops because I logged out in Windows. When I reload my username and password, it works again. How can I make it always on, even when I reboot or log out?</p>
  
  <pre><code>import time
  import random
  import os
  from pathlib import Path
  from SMWinservice import SMWinservice
  import sys
  import win32serviceutil
  import win32service
  import win32event
  import servicemanager
  
  class FlaskService(SMWinservice):
      _svc_name_ = "FlaskServiceSmartFactory01"
      _svc_display_name_ = "Flask Service Smart Factory01"
      _svc_description_ = "Python service framework Smart Factory01"
  
      def start(self):
          self.isrunning = True
  
      def stop(self):
          self.isrunning = False
  
      def main(self):
          os.chdir(os.environ['SMARTFACTORYPATH'])
          os.system('C02_RestAPI.py')
  
  if __name__ == '__main__':
      if len(sys.argv) == 1:
          servicemanager.Initialize()
          servicemanager.PrepareToHostSingle(FlaskService)
          servicemanager.StartServiceCtrlDispatcher()
      else:
          win32serviceutil.HandleCommandLine(FlaskService)
  </code></pre>