473,382 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

console application close

When I run a console application by bringing up a dos window and then running
the exe, I noticed that when I close the dos window, this kills my
application. My question is, Is there some way that I can detect this and
ensure that I can control how my application closes? I need to ensure that
the disk file I have open that I properly close it before the application
goes away.

Thanks in advance for your assistance!!!
Nov 17 '05 #1
2 26851
There is an event that you can bind to which will let you perform tear-down operations, although explicitly disposing of a managed
object may be useless as the framework will ensure proper clean-up when the appdomain is unloaded anyway. If your using unmanaged
file handles (or if you just want to be 100% sure that the memory is cleaned), then try binding to the event:

System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess();
process.Exited += new EventHandler(OnExited);

// Note, you may also want to dispose of the "process" object when the event is fired.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Jim Heavey" <Ji*******@discussions.microsoft.com> wrote in message news:5A**********************************@microsof t.com...
When I run a console application by bringing up a dos window and then running
the exe, I noticed that when I close the dos window, this kills my
application. My question is, Is there some way that I can detect this and
ensure that I can control how my application closes? I need to ensure that
the disk file I have open that I properly close it before the application
goes away.

Thanks in advance for your assistance!!!

Nov 17 '05 #2

"Jim Heavey" <Ji*******@discussions.microsoft.com> wrote in message
news:5A**********************************@microsof t.com...
When I run a console application by bringing up a dos window and then
running
the exe, I noticed that when I close the dos window, this kills my
application. My question is, Is there some way that I can detect this and
ensure that I can control how my application closes? I need to ensure
that
the disk file I have open that I properly close it before the application
goes away.

Thanks in advance for your assistance!!!


I would suggest you to install a Console Control handler that catches the
Console Close event.

enum CtrlType {
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
}

private delegate bool EventHandler(CtrlType sig);

// Sample Console CTRL handler
private static bool Handler(CtrlType sig)
{
bool handled = false;
switch (sig)
{
case CtrlType.CTRL_C_EVENT:
case CtrlType.CTRL_LOGOFF_EVENT:
case CtrlType.CTRL_SHUTDOWN_EVENT:
case CtrlType.CTRL_CLOSE_EVENT:
{
// Clean-up code goes here
}
// return false if you want the process to exit.
// returning true, causes the system to display a dialog
// giving the user the choice to terminate or continue
handled = true;
break;
default:
return handled;
}
return handled;
}

[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler (EventHandler handler,
bool add);

...in Main()...
// Install handler
_handler += new EventHandler(Handler);
SetConsoleCtrlHandler(_handler, true);
....
Willy.
Nov 17 '05 #3

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

Similar topics

7
by: Job Lot | last post by:
How can I terminate console application in Try…Catch…Finally…End Try block so that code in Finally gets executed. If I use End statement Finally does not get executed. Following is my code...
2
by: Martín Marconcini | last post by:
Hello there, I'm writting (or trying to) a Console Application in C#. I has to be console. I remember back in the old days of Cobol (Unisys), Clipper and even Basic, I used to use a program...
5
by: Mullin Yu | last post by:
i want to build an application of both gui and batch interface by using windows application project. i check either passing any args or not. if no, then open the gui application. if yes, use the...
2
by: PeteZ | last post by:
Hi, This may seem like a silly question but I'll ask anyway 8^) I have a C# Windows "Console Application" that when a fatal error occurs, I'll log the error and I then want to stop the...
8
by: Alison | last post by:
Hi, Al I am trying to design a user interface which provides both menus and toolbars for some users to click on whatever they want to do, at the same time, I would like to have a console window...
1
by: Peter Oliphant | last post by:
I have a C++ Console application being written using VS C++.NET 2005 Express. It never crashes and seems to be working just fine. However, when I close the application via the Console 'X' exit...
6
by: tony | last post by:
Hello! When you have windows forms you have the same possibility as when you have a Console application to use Console.Writeln to write whatever on the screen. Now to my question: Is it...
10
by: Stephany Young | last post by:
When one uses the System.Diagnostics.Process.Start method to launch a common or garden Console application, one can set the WindowStyle property of the StartInfo object to ProcessWindowStyle.Hidden...
12
by: Dilip | last post by:
Hi All I have a server based C# console application. This application must hide its console window when its launched out on the field. So I dutifully P/Invoke'd FindWindow/ShowWindow...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.