|
-
Jan 12th, 2010, 01:26 AM
#1
Thread Starter
Addicted Member
Detect windows shutdown in consol application
Hi,
I have an application and this application needs to display a message when the window shutdown happen. How can i detect that the system is shutting down from a C# program
Code:
namespace ConsoleApplication1
{
public partial class Program
{
static void Main(string[] args)
{
string regMatch = "";//string to search for inside of text file. It is case sensitive.
string readtext = "";
string path = "";
bool persistantfilepresent = false;
bool status = false;
try
{
readtext = readroutetable(); // Read the windows routing table
//path = "d:\\file.txt";
//deletefiles(path); //Delete the output dump file
path = "d:\\madavas1.txt";
deletefiles(path); //delete madavas1 files
createfiles(path); // Create a file to write to madavas1.txt.
path = "d:\\madavas2.txt";
deletefiles(path); //delete madavas2 files
createfiles(path); // Create a file to write to madavas2.txt.
path = "d:\\madavas1permanent.txt";
createfiles(path); // Create an empty file for storing the peristant route
regMatch = "Active Routes:";
ActiveRoutes(regMatch, readtext);//create mdavas1.txt for persistent route
regMatch = "Persistent Routes:";
Persistentroute(regMatch, readtext);//create mdavas2.txt for active route
readrange();
currentgateway(); // Read current gateway values
status = comparerange();
}
persistantfilepresent = checkpersitantroute();
writepersistantroute();
// Here I want a while loop to check whether the system shut down is there
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
Console.ReadLine();
}
}
}
If you found my reply helpful, please rate me

-
Jan 12th, 2010, 03:10 AM
#2
Thread Starter
Addicted Member
Re: Detect windows shutdown in consol application
i tried some thing like
Code:
internal static bool shuttingDown;
private const int WM_QUERYENDSESSION = 0x0011;
protected override void WndProc( ref Message m )
{
if( m.Msg == WM_QUERYENDSESSION)
{
// You're shutting down
shuttingDown = true;
}
base.WndProc( ref m );
}
but i am getting following error
Code:
Error 1 The type or namespace name 'Message' could not be found (are you missing a using directive or an assembly reference?)
Any idea wats wrong with my modification
If you found my reply helpful, please rate me

-
Jan 12th, 2010, 03:28 AM
#3
Thread Starter
Addicted Member
Re: Detect windows shutdown in consol application
Am i missing some header file?
If you found my reply helpful, please rate me

-
Jan 13th, 2010, 02:10 AM
#4
Thread Starter
Addicted Member
Re: Detect windows shutdown in consol application
I have modified the project to include a window form. Now i am able to detect when the shutdown happening. But i am having another problem now
Code:
public partial class frmdualconection : Form
{
private const int WM_QUERYENDSESSION = 0x0011; //Shut down
public bool isShuttingDown = false;
public frmdualconection()
{
InitializeComponent();
logicalflow();
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_QUERYENDSESSION)
{
isShuttingDown = true;
}
base.WndProc(ref m);
}
}
In logicalflow(), i have a while loop and if the program is running in while loop section and a shutdown comes, that is not detected . how can i handle this?
If you found my reply helpful, please rate me

-
Jan 13th, 2010, 02:28 AM
#5
Re: Detect windows shutdown in consol application
Have you tried inserting a DoEvents in your loop?
-
Jan 13th, 2010, 02:31 AM
#6
Thread Starter
Addicted Member
Re: Detect windows shutdown in consol application
This is my while loop
Code:
while (statusmada == false)
{
path = "d:\\madavas2.txt";
deletefiles(path); //delete madavas2 files
createfiles(path);
readtext = readroutetable(); // Read the windows routing table
regMatch = "Active Routes:";
ActiveRoutes(regMatch, readtext);
Thread.Sleep(1000); // sleep the application for 1 second
currentgateway();
statuszain = comparerangeforzain(); if (statuszain == true)
{
statusmada = comparerangeformada();
}
if (statusmada == true)
{
break;
}
}
Here how i will insert a do event loop. I am not that good with the c#.
If you found my reply helpful, please rate me

-
Jan 13th, 2010, 03:17 AM
#7
Re: Detect windows shutdown in consol application
You can try inserting it after this line.
Code:
if (statusmada == true)
{
break;
}
BTW, what did you mean when it is not detected when you are in a loop? How are you suppose to detect it?
-
Jan 13th, 2010, 03:27 AM
#8
Thread Starter
Addicted Member
Re: Detect windows shutdown in consol application
as in previous post, i am using followwing code to detect the shutdown
Code:
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_QUERYENDSESSION)
{
isShuttingDown = true;
}
base.WndProc(ref m);
}
When the program is in the while loop, if a shut down comes, the break point put inside above fucntion is not hit. So what do event i have to put it in the loop so that when a shutdown messge happens, i get out of the loop
Did u mean put the code in do while loop?
like this ?
Code:
do
{
path = "d:\\madavas2.txt";
deletefiles(path);
createfiles(path);
readtext = readroutetable();
regMatch = "Active Routes:";
ActiveRoutes(regMatch, readtext);
Thread.Sleep(1000);
currentgateway();
statuszain = comparerangeforz();
if (statuszain == true)
{
statusmada = comparerangeformada();
}
if (statusmada == true)
{
break;
}
} while (statusmada == false);
Last edited by makdu; Jan 13th, 2010 at 03:30 AM.
If you found my reply helpful, please rate me

-
Jan 14th, 2010, 02:37 AM
#9
Thread Starter
Addicted Member
Re: Detect windows shutdown in consol application
Is there a way to detect the shutdown intiated by windows in consol application?
If you found my reply helpful, please rate me

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|