Where to reset GPIO in console app on close?

Hello all,

Just implemented the GPIO functionality. Nice. Very nice! My problem/question is, where is the code put to turn off outputs before the console app quits? I put the following code in the App.Stop event but it does not appear to work - the LED does not turn off.

#If TargetLinux And TargetARM Then
  GPIO.DigitalWrite( k_DTR_LEDPin, GPIO.Off )
  GPIO.DigitalWrite( k_Relay1_Pin, GPIO.Off )  //Set low
  GPIO.DigitalWrite( k_Relay2_Pin, GPIO.Off )  //Set low

Probably important to know is that the app is being “killed” using the Task Manager. Maybe I have the code in the wrong place? But, in addition to having it in the Stop event, I also have it in the serial error event. This code checks to see if the port is still open, if not it turns off the LED.

#If TargetLinux And TargetARM Then
  If Me.ComPortOpened = False Then 
    GPIO.DigitalWrite( k_DTR_LEDPin, GPIO.Off )
  End If

Can anyone point me in the correct direction?


Console apps do not have a Stop event. Perhaps you mean a Service app? If so, Service apps are for Windows Services and the Stop event is only called when the Service is stopped (or the computer shuts down).

A console app quits when you reach the end of its Run event. So that would be the place to call a method that does your cleanup. If you are “killing” the app, the OS terminates it immediately so you don’t have control over what code it runs.

Thanks for your response Paul.
It is a Service/Daemonized app.
What is the best way to stop a service app, or daemonized app? I had not thought of this - only the opposite, keeping it running!


First, as this is a Raspberry Pi app, there is no need to make it a ServiceApplication as those are for MS Windows. Just make it a ConsoleApplication.

Unfortunately it doesn’t look like there is a good way for a Linux app to catch a termination signal. There are some suggestions in this thread:


Or you could build your own mechanism in for your app to listen or look for something that tells it should quit. I suppose maybe a special file with a “QUITME” value in it that your app checks for might work? Or maybe it listens on an IPC socket for a message?

Thanks Paul.
I am going to add a push switch connected to the GPIO for shutdown. That works when physically on-site, but not when away. Is it possible that a “helper app” be used to send the app a signal to shutdown? I would assume so - even writing to a text file that the main app reads periodically.

Also, although I am running this on a Pi right now, I am targeting Linux in general since this will eventually be used in other SBC’s and possibly desktop Linux instances.

Thank you again for your reply and continued support!