Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keep app running in background and capture close event

Status
Not open for further replies.

alan6895

Programmer
Aug 3, 2007
48
US
Hi all.
I thought I posted this yesterday, but it doesn't show up in "My Threads" on the board. If this is a duplicate and the other is somehow hiding from me, I apologize.

I have two questions about some VB .NET programming I'm working on. I have an app that runs at startup (from the Startup folder) and collects some system data. Right now, it runs and then quits once the data is sent to a SQL server.

The first problem is that I'd like to keep the application running, though not doing anything, until it's told to close. I'd like to do this so I can capture the Close event (see the second problem, below). How can I make the app stay open? It's a console application (no need for a form), so it would also need to run in the background hidden from view. I'd like it to not show up in the Applications tab in Task Manager (Processes is fine, obviously).

The second problem is that once it's running, I want to be able to do some final data collection when it is told to close by a logoff or shutdown. I tried to do something like "Private Sub Closing(ByVal...) Handles Me.Close". First, I don't know if Me.Close exists and seconds, VS flags Handles as requiring a WithEvents somewhere. I can't get WithEvents to work anywhere or even know what I'm supposed to do with it. Anyone know how to do this? I'm sure there's a way. Also, related, if I can capture the close event and do something, is there a way to tell why it closed (shutdown, logoff, manually closed)?

Thanks, and please let me know if something is unclear!
 
With everything that you need it to do, you should use a Windows Service instead of a Console. The Windows Service provides you with all the abilities you want to provide (persistance, reason for stopping of service)

The quick one line answer is to create a new project using the Windows Service template in Visual Studio. That will get you started. However, this will walk you through how to get it working and installed...



im in ur stakz, overflowin ur heapz!
 
I had thought about using a service, but figured it was too complicated. Looks like it's very simple, though. Thanks for the advice! I'll give it a try tonight.
 
yeah, windows services are a breeze in .net :)

im in ur stakz, overflowin ur heapz!
 
macleod:
Thanks for the advice. Th service seems to be working alright.

I did notice two issues between what I need it to do and what it's doing.

First, when I capture the username, it gets the username of the user running the service. Right now, it's running as Local Service because I don't know how to make it run as the user who logs on. So, do you know either a) how to make it run as the logged-on user or b) how to get the logged on user's username (not the name of the account the service is running as)?

Second, the service is starting when the computer turns on and then stops when it shuts down. Can I make a service run when the user logs on an stop when the user logs off? That's what I need it to do. Plus, it looks like the network stack shuts down before the service, which means that on a shutdown, I get no data sent to SQL server. That isn't a huge issue, though.

Thanks!
 
For the first issue, this might get you pointed in the right general direction...


For the second issue, I would think you could create a user logon script that would run a batch file containing:

<code>
net start <service_name>
</code>

and a logoff script that would run:

<code>
net stop <service_name>
</code>

This is of course if you're on an Active Directory domain. I don't think local computers have logon and logoff script options (but I could be wrong).
 
Fantastic. Works like a charm now. Thanks for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top