From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

LabVIEW as a Service - process stays in memory despite finishing execution

 

I have a fairly interesting/unique problem with trying to run a LabVIEW built executable as a service using srvstart (http://www.nick.rozanski.org.uk/services). Srvstart allows you run any application as a service (similar to srvany or firedaemon) but it has the advantage that when you shutdown the service, you can set it to send a WM_CLOSE message which can be picked up in LabVIEW using the Application Instance Close? filter event and shutdown the service gracefully without killing it (as is the case with srvany).

 

To debug this, I put a simple file write into the Application Instance Close? and Panel Close? events, my 'Exit' case and in a sequence structure at the very end of my application. If the application shuts down gracefully, a file will be created for at least the 'Exit' case and the the final file write.

 

When I run the application in the LabVIEW development environment or as a normal executable (by double clicking on the EXE), it shuts down gracefully (by queueing up an 'Exit' command and executing the 'Exit' case) when I close the front panel (which is just an empty front panel - no controls/indicators as it runs without a UI as a service) - it creates the files and the application closes (and the process in task manager disappears).

 

When I run the application and try to shut it down by using 'End Task' or run the application as a service. This should fire the Application Instance Close? filter event, which is then discarded (with silent shutdown) which will should queue up an 'Exit' command and shut down the software gracefully.

 

What actually happens is that If I'm running the EXE normally and I 'End Task', the front panel closes but the application process stays in the system (i.e. in task manager). None of the files are written so the application hasn't gracefully shutdown. I then have to kill the process in task manager.

 

When I'm running as a service and 'Stop' the service, the files for Application Instance Close?, my 'Exit' case and the last call get created, the memory usage goes from 30to 80mb and the process stays in memory (in task manager) - the service doesn't respond and I have to shut it down via task manager. I call 'Quit LabVIEW' right at the end of my VI, just after the last call file is created, if I disable this, the same thing happens but the memory usage doesn't go up. In both cases though, the service/application doesn't respond.

 

I have to be a little careful about what I post in terms of VIs/screenshots for IP reasons but I'll try to illustrate my setup:

 

TroubleshootingLabVIEWasaService.png

 

In terms of other considerations, the VI does some asynchronous calls for handling TCP connections but these will finish execution when the main event queue is destroyed and the problem still occurs even if no TCP connections occur (and hence no calls are made to the asynchronous VI). I'm running LabVIEW 2011 SP1 on a Windows 7 64-bit.

 

I tried to use the Desktop Trace Execution toolkit to help debug the issue but it seems that the VI Server shuts down and doesn't show me any trace information after I tell the service to Stop.

 

The things I don't understand are:

- If I 'End Task' on the EXE, shouldn't that fire the Application Instance Close? filter event in LabVIEW? I understand that Windows would give a 'This application is not responding...' message if it then didn't shutdown or if I didn't discard the event but I don't understand why that event isn't being captured/discarded on my EXE.

- Why doesn't the process end when the VI has finished executing - even though I call the 'Quit LabVIEW' VI as the last call in my application?

- Finally, why does the memory usage go from 30mb to 83mb depending on whether or not I call the 'Quit LabVIEW' VI (but the process still stays in Task Manager)?

 

The key one is the second one - as this is the one that stops me from being able to stop/restart the service from 'Services'. I'd like to be able to use my 'Exit' case to try and send a last ditch 'Hey I'm shutting down now' message before the PC switches off by gracefully shutting down my service (and you know - because it's better to shutdown things in a controlled manner - hence the reason for using srvstart in the first place!)

 

If anyone can help shed some light on this I'd really appreciate it as I've been trying to debug this for a lot longer than I'd like and despite asking around a few people not been able to come to any sort of solution.


LabVIEW Champion, CLA, CLED, CTD
(blog)
0 Kudos
Message 1 of 23
(5,353 Views)

I never really did get to the bottom of this despite spending/wasting a lot of time on it.

 

The solution I had to use was to use a System Exec.vi call to taskkill the executable after my graceful shutdown code ran (i.e. replace the Quit LabVIEW with a System Exec call). This allowed my graceful shutdown code to run correctly and kill the process at the end so it would be shown as 'Stopped' in the Windows Services window (which means I can start/stop/restart the service through that which is what I wanted).

 


LabVIEW Champion, CLA, CLED, CTD
(blog)
0 Kudos
Message 2 of 23
(5,308 Views)

Same problem. My Labview application runs several instances of "Vision Builder.exe". When I use srvany to run my applicaiton as a service, the vision builder processes do not stop when the service is stopped. I changed to using NSSM (the "Non-Sucking Service Manager") and when I stop the service, my labview application process stops along with my Vision Builder.exe processes. So that is an improvement. However, my application is still not shutting down gracefully. I tried using the "Application Instance Close?" event but it does not appear to be executing when the service stops my application. I have the "SilentShutdown" and "Discard?" nodes set to "True" but I've tried other combinations as well--same behavior. I tried using the "Application Instance Close?" event to write a text file but that also does not happen... so I'm pretty sure this event is not executing when the service is stopped and my application process is stopped.

 

Any ideas? Is there a service manager I can use that Labview will respond with an application close event?

 

The previous thread said to replace the "Quit Labview" VI with the System Exec and Taskkill command. I tried this but I have the same behavior. I am currently shutting down my application after my Event Loop is stopped (Event Loop can be exited with a manual button on my FP or by the "Application Instance CLose?" event which does not appear to be working) and I use the FP.Close = True method.

 

Chris Walker
Certified Labview Developer
0 Kudos
Message 3 of 23
(5,065 Views)

Update: I configured NSSM to send a WM_CLOSE to the Labview application when the service was stopped, and then wait 10 seconds, and then call the TerminateProcess() method. From watching the processes, neither the "Application Instance Close?" nor the "Application Instance Close" events were registered because I have specific code there that will execute a graceful shutdown and log a text file if they were. However, 10 seconds later everything shutdown from the TerminateProcess() method. I would prefer to find a way to do a graceful shutdown when running from a service. Other possible commands are "WM_QUIT" and "Ctrl-C" (from NSSM) but I do not think these Labview events respond to these either.

Chris Walker
Certified Labview Developer
0 Kudos
Message 4 of 23
(5,054 Views)

Since about LabVIEW 8.5 it is a very bad idea to add the Application Close? filter event and expect LabVIEW Quit to still do anything! It simply behaves as No Operation when this filter event is registered in any event structure.

 

The proper way to handle this is to make the LabVIEW code terminate gracefully (terminate every loop in there) and as last operation close the Front Panel explicitedly with the VI Server method.

 

LabVIEW Quit is btw. anyhow only a emergency bandaid. It is really analogous to crashing your car in a wall to make it stop. Pretty effective but also dangerous for various reasons. The proper way to terminate a LabVIEW built application is and has always been to make sure every loop has terminated and then close every and any front panel of your application as last measure. An open front panel (even when hidden) will keep your executable alive forever.

Rolf Kalbermatter
My Blog
0 Kudos
Message 5 of 23
(5,047 Views)

I agree and that is what I am trying to do. My application does exit gracefully if the Front Panel button is used (closes every loop, uses the FP.Close VI Server method, etc.). However, I need my application to run as a service and cannot find a way to get it to shutdown gracefully if the service is stopped. That is my problem and that is why I was trying to have Labview to "catch" the application close event so that it can use this to also shutdown gracefully rather than just terminate. When my application runs as a service, there is no Front Panel and ocassionally this service (my aplication) needs to be stopped and restarted (mostly for maintenance purposes). 

 

So my question is: If the service is stopped, how can I get Labview to respond to this and exit gracefully rather than just terminating?

Chris Walker
Certified Labview Developer
0 Kudos
Message 6 of 23
(5,042 Views)

It's been a while now since I did this and only saw that it had been replied to because it was on the front page of threads. I did end up finding a solution but I never managed to get the service to respond to any sort of shutdown message from windows (e.g. App Instance Close? or WM_CLOSE etc.).

 

I ended up using SrvStart with the shutdown_method=command, shutdown="ServiceShutdown.exe" where ServiceShutdown.exe is an application I wrote that sends a shutdown command to my service over TCP (my application already responds to TCP commands). I include this with my application and it gets called whenever I stop the service from the services manager.

 

It's a stupid hacky solution but it works pretty effectively.


LabVIEW Champion, CLA, CLED, CTD
(blog)
Message 7 of 23
(5,033 Views)

Hello Sam,

I run into the same Problem.
This "https://decibel.ni.com/content/docs/DOC-41403" seems to lead to an solution, but at the end it is not working (see my comment in the post).
The Problem is when you provide the application to customers, they will simply shutdown windows, and than nssm will kill the application and I worry that files can be corrupted (until now it has not happened).
In the nssm (non-sucking-service-manger; https://nssm.cc; the program I use to make my applications to a service)  will be included the possibility to define "Actions taken before/after starting or stopping the service" in Version 3. I hope this will happen soon.

0 Kudos
Message 8 of 23
(4,827 Views)

Hello,

Now there is a "Featured pre Release" of nssm (https://nssm.cc/download).
Now is possible to call an exe before killing the Service. You can write an LabVIEW exe which end your Service gracefully. For expamle via VI-Server. It works!

0 Kudos
Message 9 of 23
(4,558 Views)
Hello Everyone, I am new user to NSSM, I am planning to create LabVIEW EXE and deploy it as Windows services; I use NSSM for this purpose. NSSM state that it will try to restart the application on crash automatically. I made my application (LabVIEW.exe) to forceful exit (or stimulate crash) and couldn’t get the application to restart... I gave the default configuration as mentioned in below snippet. Can you please tell if I am missing something? It would be great if I get this feature working.
0 Kudos
Message 10 of 23
(4,473 Views)