locked
Windows service suddenly stops progress RRS feed

  • Question

  • I have a windows service which will do some operations on an interval x in hours. Whenever the service starts and ends it will write start time and end time in an text file . I have started the service 3 days back it works reguarly for an interval suddenly the service progress stops but the service is in running mode in local services.It fails to write in text file.I have checked the event viewer too but couldnt get any idea . But when in restart it works fine running properly.Could you please help me out
    Tuesday, February 24, 2015 9:54 AM

Answers

  • Services work differently than console apps when it comes to security and that is generally the problem.  If you want to share resources in a service then you generally need to explicitly set the security of the shared resource (mutex, file) when you create it otherwise other processes will have a problem access it. For a mutex you should use the constructor that allows you to specify security.  Be sure to read the notes in MSDN about what permissions you need to set on each side to avoid an error.

    For the file you will want to ensure that you either use the same account for both services or that you give everyone the necessary file permissions so that both processes can access it.  The File.Create method allows you to do this.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by Kristin Xie Friday, March 6, 2015 4:52 AM
    Wednesday, March 4, 2015 2:48 PM

All replies

  • Do you log your program? Can you share the code here on what are you running in your code?

    chanmm


    chanmm

    Tuesday, February 24, 2015 10:32 AM
  • I have logged in text file for every start and then once the task done. This is will be continued for every time.

    Its a huge code  and am not supposed to share too. Its been working for past 3 days dont know why it stops(progress) but still in running

     
    Tuesday, February 24, 2015 11:42 AM
  • Are you leaving the file open the entire time or opening and closing it each time you write?  Generally not a good idea to leave a file open indefinitely in a service.

    Also you should be using a service timer and not something else. If you aren't willing to post any code for us then we aren't going to be able to help you too much.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Tuesday, February 24, 2015 4:31 PM
  • @ selva173,

    My suggestion is add some logging, either using the EventLog or a logging framework (Log4Net).

    According to the log file to find the error.  Thanks


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 25, 2015 8:44 AM
  • Hi,

    I have found the cause which was due to the mutual exclusion(mutex). I have enabled for dot net mutex for a partiicular resource(xml) which was used by two processes say P1 and P2.

    I have made some research with mutex. Let me explain what exactly happens 

            I have created two console applications say p1 and p2 both requesting the same resource (writing a text file).When P1 locks the resource , P2 waits (other console) waits till P1 releases the mutex it holds. Once it releases the mutex P2 gets access to the resource and vice- versa.This is what happens in console.

    Then i have created a two windows service say S1 and S2 . Same process as mentioned above . When S1(service 1) is locked the resource say for 10 seconds and write the info in text file  and releases the mutex it holds. S2 didnt get access to resource even when the resources are released . It was in running mode but not doing the operation (writing text file periodically was its work).

    Dont know why? Can anyone suggest some solutions

    ~Selva

    Wednesday, March 4, 2015 9:57 AM
  • Services work differently than console apps when it comes to security and that is generally the problem.  If you want to share resources in a service then you generally need to explicitly set the security of the shared resource (mutex, file) when you create it otherwise other processes will have a problem access it. For a mutex you should use the constructor that allows you to specify security.  Be sure to read the notes in MSDN about what permissions you need to set on each side to avoid an error.

    For the file you will want to ensure that you either use the same account for both services or that you give everyone the necessary file permissions so that both processes can access it.  The File.Create method allows you to do this.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by Kristin Xie Friday, March 6, 2015 4:52 AM
    Wednesday, March 4, 2015 2:48 PM