473,800 Members | 2,613 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Detecting Windows Shutdown in Console Application -- How?

I have a console application that spawns off several C# threads. It
needs to react to shutdown of the system by performing some end of
program activities including writing some files and closing some other
open files and then shutting down gracefully.

I've done a lot of searching online and there seem to be a lot of
potentially interesting leads, an OnShutdown is mentioned, as is a
SessionEnding event, but I'm not sure what the normal procedure is. I
suspect I need to create an event delegate and then register for this
session ending event, but I'm not exactly sure how to do it and I was
hoping to find a code sample. Does anyone know where I can find one?
Thanks.

David W. Griffin
Lockheed Martin Aeronautics Company

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
5 14725
You can't use these events from Console applications.
SessionEnding/SessionEnded can only be used in windows applications while
OnShutdown is for Services only. Note that as far as I know there is no way
to keep a cmd shell running once the shutdown activity is started and the
system kill's the (cmd) process.

Willy.
"David Griffin" <ca***********@ yahoo.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
I have a console application that spawns off several C# threads. It
needs to react to shutdown of the system by performing some end of
program activities including writing some files and closing some other
open files and then shutting down gracefully.

I've done a lot of searching online and there seem to be a lot of
potentially interesting leads, an OnShutdown is mentioned, as is a
SessionEnding event, but I'm not sure what the normal procedure is. I
suspect I need to create an event delegate and then register for this
session ending event, but I'm not exactly sure how to do it and I was
hoping to find a code sample. Does anyone know where I can find one?
Thanks.

David W. Griffin
Lockheed Martin Aeronautics Company

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2
David:

Check out the Win32 call SetConsoleCtrlH andler. This may help.

John
Willy Denoyette [MVP] wrote:
You can't use these events from Console applications.
SessionEnding/SessionEnded can only be used in windows applications while
OnShutdown is for Services only. Note that as far as I know there is no way
to keep a cmd shell running once the shutdown activity is started and the
system kill's the (cmd) process.

Willy.
"David Griffin" <ca***********@ yahoo.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
I have a console application that spawns off several C# threads. It
needs to react to shutdown of the system by performing some end of
program activities including writing some files and closing some other
open files and then shutting down gracefully.

I've done a lot of searching online and there seem to be a lot of
potentially interesting leads, an OnShutdown is mentioned, as is a
SessionEnding event, but I'm not sure what the normal procedure is. I
suspect I need to create an event delegate and then register for this
session ending event, but I'm not exactly sure how to do it and I was
hoping to find a code sample. Does anyone know where I can find one?
Thanks.

David W. Griffin
Lockheed Martin Aeronautics Company

*** Sent via Developersdex http://www.developersdex.com ***


Nov 17 '05 #3
On 2005-07-06, David Griffin <ca***********@ yahoo.com> wrote:
I have a console application that spawns off several C# threads. It
needs to react to shutdown of the system by performing some end of
program activities including writing some files and closing some other
open files and then shutting down gracefully.

I've done a lot of searching online and there seem to be a lot of
potentially interesting leads, an OnShutdown is mentioned, as is a
SessionEnding event, but I'm not sure what the normal procedure is. I
suspect I need to create an event delegate and then register for this
session ending event, but I'm not exactly sure how to do it and I was
hoping to find a code sample. Does anyone know where I can find one?
Thanks.

David W. Griffin
Lockheed Martin Aeronautics Company

*** Sent via Developersdex http://www.developersdex.com ***


As was mentioned, you need to use the win32 SetConsoleCtrlH andler api to
set up a callback. The code looks something like:

using System;
using System.Runtime. InteropServices ;

private enum ControlEventTyp e
{
CtrlCEvent = 0,
CtrlBreakEvent = 1,
CtrlCloseEvent = 2,
CtrlLogoffEvent = 5,
CtrlShutdownEve nt = 6,
}

private delegate bool HandlerDelegate (ControlEventTy pe dwControlType);

[DllImport ("kernel32", SetLastError=tr ue)]
private static extern bool SetConsoleCtrlH andler (
HandlerDelegate HandlerRoutine,
bool Add);
public class ConsoleApp
{
private static HandlerDelegate controlHandler;

public static void Main ()
{
// create the delegate and add the handler
controlHandler += new HandlerDelegate (ControlHandler );
SetConsoleCtrlH andler (controlHandler , true);

// do you cool stuff

// clean up...
SetConsoleCtrlH andler (controlHandler , false);
}

private static bool ControlHandler (ControlEventTy pe controlEvent)
{
// if you handle the signal, return true - else false
switch (controlEvent)
{
case ControlEventTyp e.CtrlCEvent:
....
break;
case ControlEventTyp e.CtrlShutdownE vent:
...
break;

...
}
}
}

Anyway, this is obviously untested air code - but I do have a working
sample somewhere, if you really need it I'll dig it out. I actually
have a class with most of the win32 console api calls in it :)

--
Tom Shelton [MVP]
Nov 17 '05 #4

<jp******@mvisi ontechnology.co m> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.com...
David:

Check out the Win32 call SetConsoleCtrlH andler. This may help.

John


That's right you can install your own handler using PInvoke, v2.0 makes it a
bit easier to do using the added Console support.

Thanks,
Willy.
Nov 17 '05 #5
Thanks. It's hard to even know what to look for -- what term to search
for. It seems like something every well behaved application would want
to do but none of my books seem to cover this and I've been less than
successful on line too. Hopefully this will steer me in the right
direction.

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3010
by: Les Desser | last post by:
Is there any way to detect within A97 when Windows is trying to shut down, so I can close down the application gracefully? We currently have a trap in the main menu asking the user to confirm if they want to shut the application. If the user is not at his desk and power fails and the UPS instructs Windows to close, then Windows will not shut down till the Access dialog is dismissed. Result = a flat battery and a crashed PC. Thanks.
2
10985
by: Chris | last post by:
Hi, Currently, I have a console application written in C# and an unmanaged legacy DLL written in VC++ 6.0. In the DLL's previous application, when an event occurs in the DLL, a windows message would be sent to the host GUI application via PostMessage to WM_USER + X. The GUI application would then execute a function to retrieve data from the DLL. With this new setup, I have a console application as the host. I retrieved the handle...
5
11261
by: Barry Mossman | last post by:
Hi, can I detect whether my class is running within the context of a Console application, vs say a WinForm's application ? also does anyone know whether the compiler or runtime is smart enough to avoid the overhead of writing to the console if it is not visible, eg I am running inside a WinForm application. thanks
6
8766
by: carbon_dragon | last post by:
Ok, so here is the problem. I'm working on a headless server program implemented as a .NET C# Console project. There is a UPS mounted to this server (though not a windows compliant UPS). I can only talk to the UPS over a special device driver. Through this device driver I can detect that the UPS is going to notify Windows 2000 server to shut down. So I start doing a graceful termination. But Windows shuts down pretty quickly and there...
3
15031
by: inpreet | last post by:
I am trying to build a console application in C#.Net. This application is suppose to run in background without user interaction. How can I hide console to appear?
3
3238
by: rdemyan via AccessMonster.com | last post by:
I have a forced logoff routine in place that works well unless an Access messagebox is showing in the front-end that the routine is trying to shut down. It doesn't work use Access commands to shut down the application. Is there a way to use a Windows API to shutdown the application? I'm hoping that, like the task manager, a Windows API shutdown would work even if a modal form is displayed in the application it is trying to shutdown. ...
1
4342
by: Ajak | last post by:
Hello, How do I scan for keypress in vb.net console application? E.g: A menu is presented to user where they can proceed by pressing the corresponding letter (A, B C...etc), without having to press 'Enter' key. Is this possible? tq
2
2628
by: Chris Mullins | last post by:
For some reason the question, "Can I make an EXE that is both a Windows Service and a Console Application?" has come up quite a bit for me over the last few weeks. I've been doing this for years, but for some reason I've never seen it documented anywhere. C# (or VB.Net) is perfectly happy to create an EXE that can be: 1 - Both a WinForms App or a Windows Service 2 - Both a Console App or a Windows Service
4
6272
by: Dinsdale | last post by:
I am writing a small console application that runs fine unless I am re- directing the output to a file (i.e. c:\ app.exe >>output.txt) . I have determined that the issue is caused by the Console.Clear() function. When piping to a file, the console.clear causes the following error: Unhandled Exception: System.IO.IOException: The handle is invalid. at System.IO.IO__Error.WinIOError(Int32 errorCode, String maybeFullPath)
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10279
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7582
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6815
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.