Terminating Processes from a Windows Service Cleanly (c#).

Hi,

Recently, I have created a Windows Service in C#; that calls a process (writen previously in c). It starts fine, however im unable to terminate the process cleanly, when the Onstop() is called.

 

Process.Kill() works, but prevents my process from shutting down cleanly (loses data - that I need). Ideally I need to use the CloseMainWindow() method... but this does not seem to work within a service (as a service does not run an interactive GUI... and does not take advantage of the WM_CLOSE message)...

Is there a way to send the process a signal; to enable a clean termination


Any ideas

Best Regards

Robert 



Answer this question

Terminating Processes from a Windows Service Cleanly (c#).

  • Chibos

    A Mutex is a solution, but i don't see why a C program is needed

    You can just use the ServiceController class of the .NET Framework as explained in the article i gave in my first place.


  • Brice FROMENTIN

    Thanks Phil

    Do you have any examples of using a mutex in managed code, as I dont have much experience with this aspect of Windows programming.

    Cheers for your help...

    Rob


  • kkting

    PJ. van de Sande wrote:
    A Mutex is a solution, but i don't see why a C program is needed

    You can just use the ServiceController class of the .NET Framework as explained in the article i gave in my first place.

    Read the original question again - I don't think the Service is the problem. The issue is that he's firing off a process that was "previously written in c" and in the OnStop method of his Service he wants to stop that external process. One of the ways for one Windows process to signal another is a mutex, either in Win32 code or managed code.



  • William R

    Sorry, my bad. Didn't understand that. Using a Mutex isn't so hard, .NET provides a nice managed Mutex class. Here is a list of examples and articles:



  • rxbrooks

    The kind of thing you need is a named Mutex. Because it's named, it's visible to your Service with the .NET Mutex class and in the C program with the CreateMutex Win32 API. You can set it up in the Service, launch the C program, and the C program periodically checks to see if the mutex has been set (the Service does that in OnStop).

  • David Bentz

    Hi,


    The process was originally developed in UNIX, and has been ported to Windows. Unix made it easy to start the process at boot time. I have turned to services to achieve this in Windows.


    I need the to call the process from a Windows Service (I don’t want to rewrite this code)... but I need a solution to terminate it cleanly from within a service. There does not seem to be any methods in the Process class to do this, only Process.Kill() (which kills the process unconditionally).


    The ported process can catch control messages like <control+c>, etc... Is it possible to send the process a message from the Windows service, to request this termination cleanly The process has buffered IO (writing to a file), without a clean termination the buffers are not flushed and data is lost.


    Any other ideas
    Cheers Rob 


  • TraGib

    You can create a controler, for a good article and working sample you should read this! I can't explane it better then that article.


  • Terminating Processes from a Windows Service Cleanly (c#).