1

How to convert my winform code to work as services in windows?

below my code

private BackgroundWorker worker;

    public Form1()
    {
        InitializeComponent();
        worker = new BackgroundWorker();
        worker.DoWork += worker_DoWork;
        System.Timers.Timer timer = new System.Timers.Timer(5000);
        timer.Elapsed += timer_Elapsed;
        timer.Start();
    }


    void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (!worker.IsBusy)
            worker.RunWorkerAsync();
    }

    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        //Do some sql update query here
    }
Cœur
  • 32,421
  • 21
  • 173
  • 232
  • 1
    Possible duplicate of [.NET console application as Windows service](http://stackoverflow.com/questions/7764088/net-console-application-as-windows-service) – user35443 Apr 30 '17 at 15:02
  • What are you looking to do with the service? There will be no need for the BackgroundWorker in the service and if you are using a timer to run SQL periodically the best solution depends on how often the timer ticks. See [Best Timer for using in a Windows service](https://stackoverflow.com/questions/246697/best-timer-for-using-in-a-windows-service) – null Oct 12 '17 at 04:54

1 Answers1

0

Services cannot interact with the user using a GUI in Windows.

I am quoting from this article: MSDN - Interactive Services

Important Services cannot directly interact with a user as of Windows Vista. Therefore, the techniques mentioned in the section titled Using an Interactive Service should not be used in new code.

You need to write a console application containing all of your code which is intended to run as a service, and run that as a service.

Then you need to have a separate WinForms application for interaction with the user.

Your two applications will interact with each other using some inter-process communication mechanism, probably pipes.

Mike Nakis
  • 46,450
  • 8
  • 79
  • 117