0

(I am new to Windows Services) I have an automatic windows service that fetches some records from a database table, calls a web service method for every record and inserts the return value of the method and some other information into the table. My problems are listed below:

1- When I try to start the service manually I get Error 1053 (the service did not respond to the start or control request in a timely fashion). By the way despite the error service starts and does what it has to do.

2- (I know it is a common problem but I prefer to ask it again) The service is Automatic but depends on the database connection. When the system the service is running on, is being restarted I had to start it manually. I add a Thread.Sleep() line of code to avoid this problem. What is the genuine way to solve the problem?

protected override void OnStart(string[] args)
    {
        checkRegistryForOracleNLS();

        //Creating event log
        plog = new System.Diagnostics.EventLog();
        if (!System.Diagnostics.EventLog.SourceExists("Prov"))
        {
            System.Diagnostics.EventLog.CreateEventSource("Prov", "ProvLog");
        }
        plog.Source = "Prov";
        plog.Log = "ProvLog";
        plog.Clear();
        Log("Service started at " + DateTime.Now.ToLongTimeString(), System.Diagnostics.EventLogEntryType.Information);

        class_ora_tran.int_class_ora_tran();
        DataTable dt;

        Thread.Sleep(300000); 

        while (true)
        {
            Con2DB();
            if (class_ora_tran.dbConnected())
            {
                RemoveOldRecords();
                dt = FetchRec();
                if (dt != null)
                {
                    if (dt.Rows.Count > BLKSize)
                        Bulk(dt);
                    else
                        Single(dt);
                }
            }
        }   
    }

3- Although I wrote the OnStop() method, there is no Stop function when the service is running and I want to stop it. I should also mention that my service is always in the Starting mode and never changes to Started.

Protected void OnStop(string[] args)
    {
        class_ora_tran.Disconnect();
        Log("Service stoped at " + DateTime.Now.ToLongTimeString(), System.Diagnostics.EventLogEntryType.Information);
        plog.Close();
    }

4- Every now and then, although the status is starting, my service stops doing what it has to do and does not fetch records from the table. It happened two times before, unfortunately I forgot to check Event Viewer to see if some errors happened. anybody know what is the cause? Is it related to the problems I mentioned above?

5- Every time I want to uninstall the service I had to run the uninstall command for two times! Any help?

Edit: Records are inserted into the table contentiously and the process of calling the web service and etc should be done whenever a new record has been entered to the table. I thought that a While(true) loop is needed. Any suggestion to replace this part?

Narges
  • 65
  • 2
  • 10
  • 1
    `while (true)` --> this is pretty disturbing, especially that I don't see the `break;` – T.S. Feb 22 '14 at 05:17
  • it's essential, I want the service constantly runs this part of the code. – Narges Feb 22 '14 at 06:13
  • 1
    This is horrible way to make service – T.S. Feb 22 '14 at 06:14
  • Do you have any other suggestion to do what I want to do? I'm so new in services. – Narges Feb 22 '14 at 06:15
  • Best way - service listens to the port and wakes up only when it receives the message to start processing of the existing job. Common way is to set the timer and check for jobs periodically. First way is not overly complicated but more involved. Second is trivial. You would init your service "onStart" and start the timer. There shouldn't be any logic execution on "onStart" – T.S. Feb 22 '14 at 06:18
  • Would you please show me some sample code? – Narges Feb 22 '14 at 06:22
  • See here http://stackoverflow.com/questions/246697/windows-service-and-timer And here http://stackoverflow.com/questions/12885013/windows-service-with-timer and here http://stackoverflow.com/questions/10575900/c-sharp-windows-service-with-awareness-of-system-time – T.S. Feb 22 '14 at 06:28
  • Thank you. what you mean is that I set a timer and do the process at the end of each interval? – Narges Feb 22 '14 at 06:31
  • Yes. When you detect the job and process all jobs in a queue; you can stop the timer, and process the job. Then restart the timer. I wrote one time service that would change its timer interval dynamically, depending on amount of jobs. When jobs where coming in, the timer kicked more often, when jobs where scarce it went to sleepy so it wouldn't hit DB a lot for little. Here is listener service http://www.codeproject.com/Articles/5733/A-TCP-IP-Server-written-in-C I am sure, once you clean up your `while(true)`, your service will act much better. – T.S. Feb 22 '14 at 06:36
  • Your `onStart` should be relieved of any responsibility besides initializing, like reading a config file, etc. You never finishing your onStart and then trying to run OnEnd :o)) – T.S. Feb 22 '14 at 06:40
  • Thank you @T.S.. I have this problem that the number of records inserted in the table varies in the time. Some time I have more than 1 million of records inserted in the table at once. Your approach and using dynamic intervals would handle this situation? – Narges Feb 22 '14 at 06:45
  • Forget about Dynamic intervals for now. Move your processing to Timer based approach. Then you can regulate number of records to process etc. Service usually consist of two logical parts - 1. Service workfow; 2. Actual handling of jobs. Right now you need to change your workflow. Then, you will measure performance, etc and you will be able to adjust your workflow to the needs – T.S. Feb 22 '14 at 06:56
  • @Narges Try to make your service to run every x minutes. I have added a link which i think will suit your needs. – Dexters Feb 22 '14 at 07:50
  • Also this program has a big issue with the Thread.Sleep lapses time of 50 minutes = 300,000 milliseconds which makes it unresponsive when the Thread gets into that status. That's probably why the service is not responding to the Stop command. – YeinCM-Qva Aug 04 '17 at 14:47

1 Answers1

2

I will put forth some suggestions for your service.

  1. Make your service to be a manual start. So once you install the service, you just start it manually just once.
  2. After you start, you have some good options to run your logic.

    . Run every X minutes http://www.codeproject.com/Questions/540617/windowsplusserviceplustoplusrunpluseveryplusoneplu

    . Run at hh:mm everyday Windows Service to run a function at specified time . Run immediately

  3. Put your code in a function and call this function in the Timer_Elapsed() function which ever you choose in the above link examples

  4. Use OnStart to setup the timer information, Reading Config files etc.

  5. Try using the windows Add/Remove programs to remove the service. (create an installer msi to install your service. Saves your command lines)How to create an installer for a .net Windows Service using Visual Studio

Community
  • 1
  • 1
Dexters
  • 2,419
  • 6
  • 37
  • 57
  • I can't set it to be a manual start. The system it is running on has lots of rebooting and I am not the only user works with it. I also can't make it to work at an exact time every day, what it's supposed to do is an immediate task. Thank you for your rest of reply, I'll try what you suggest. – Narges Feb 22 '14 at 07:54
  • @Narges you can play with the properties and check and some help available http://ethertubes.com/make-a-program-run-as-a-windows-service-on-boot/. You can use automatic as well. Or you can write an exe to verify that your service is running on reboot if not start it. You can search for SC commands or WMIC .. Just FYI not to confuse you much. – Dexters Feb 22 '14 at 08:02