none
Sync data in Windows Services RRS feed

  • Question

  • Hi,

    I want to run a piece of code, which should run continuously and check for some updates in changes in data.

    And What i have developed so far is

    1. WCF Services which is hosted on tcp

    2. Windows Service which is being installed on Windows

    3. Windows Forms Application,

    Now, when i run my code on WCF Service it works. but i want my code to run in windows Services, so i can just be sure that it is continuously running in the system and doing what its supposed to do.

    However, when i placed my code in  protected override void OnStart(string[] args)  it is not working infact 

    service is not starting, and reason is obvious as at the start it self my code is running and since it is running continuously, it is not able to start the service as that event is not finished.

    Please suggest.


    Thanks, Parth

    Friday, February 24, 2017 12:30 PM

Answers

  • Hi Parth,

    >> it is not working infact service is not starting, and reason is obvious as at the start it self my code is running and since it is running continuously,

    Could you share us the code in OnStart? Based on your code, it seems your code is while loop. We could not use while loop in the service OnStart which will forbid service to start.

    For running a continuous task, I suggest you start a new thread. Something like below:

    protected override void OnStart(string[] args)
    {
        Thread MyThread = new Thread(new ThreadStart(MyThreadStarter));
        MyThread.Start();
        base.OnStart(args);
    }
    private void MyThreadStarter()
    {
        //place your function here
        MyClass obj = new MyClass();
        obj.Starter();
    }

    Best Reards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Parth Rawal Monday, February 27, 2017 4:23 AM
    Monday, February 27, 2017 2:27 AM
  • Hi Parth,

    This is used to call base.OnStart with arguments, when implemented in a derived class, executes when a Start command is sent to the service by the Service Control Manager (SCM) or when the operating system starts (for a service that starts automatically). Specifies actions to take when the service starts.

    You could refer the link below for more information.

    # ServiceBase.OnStart Method (String[])

    https://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstart%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Parth Rawal Monday, February 27, 2017 6:54 AM
    Monday, February 27, 2017 5:40 AM

All replies

  • https://msdn.microsoft.com/en-us/library/ms733069(v=vs.110).aspx

    Maybe You need to look at a tutorial.

    What does a Windows form application have to do with it?

    Friday, February 24, 2017 2:23 PM
  • Hi Parth,

    >> it is not working infact service is not starting, and reason is obvious as at the start it self my code is running and since it is running continuously,

    Could you share us the code in OnStart? Based on your code, it seems your code is while loop. We could not use while loop in the service OnStart which will forbid service to start.

    For running a continuous task, I suggest you start a new thread. Something like below:

    protected override void OnStart(string[] args)
    {
        Thread MyThread = new Thread(new ThreadStart(MyThreadStarter));
        MyThread.Start();
        base.OnStart(args);
    }
    private void MyThreadStarter()
    {
        //place your function here
        MyClass obj = new MyClass();
        obj.Starter();
    }

    Best Reards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Parth Rawal Monday, February 27, 2017 4:23 AM
    Monday, February 27, 2017 2:27 AM
  • Can you please explain this? 
     base.OnStart(args);

    Thanks, Parth

    Monday, February 27, 2017 4:24 AM
  • Hi Parth,

    This is used to call base.OnStart with arguments, when implemented in a derived class, executes when a Start command is sent to the service by the Service Control Manager (SCM) or when the operating system starts (for a service that starts automatically). Specifies actions to take when the service starts.

    You could refer the link below for more information.

    # ServiceBase.OnStart Method (String[])

    https://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstart%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Parth Rawal Monday, February 27, 2017 6:54 AM
    Monday, February 27, 2017 5:40 AM