Exiting/Quitting an console application - Windows CE

A

Andrew

I have a multi-threaded console application running in Windows CE using the
..NET Compact Framework 2.0.

The following code exits the program and correctly terminates the threads
created.

static void Main(string[] args)
{
string message;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
//
// create metar serial port instance
metarCom = new SerialPort();
metarCom.ReadTimeout = 500;
//
// create task scheduler instance
taskScheduler = new ScheduledTimer();
taskScheduler.SchedulerElapsed += new
SchedulerElapsedEventHandler(taskScheduler_SchedulerElapsed);
//
// create settings instance
settings = new ManagerSettings(AppPath + "\\AgentSettings.xml");
//
// initialize settings
InitializeSettings();

//
// open metar serial port
if(metarEnabled)
metarCom.Open();
//
// create thread instance to read
// serial port
readThread = new Thread(Read);
read = true;
if(metarEnabled)
readThread.Start();
//
//
// start scheduler
taskScheduler.Start();

//
Console.WriteLine("Type QUIT to exit");
//
// loop until quit message entered
while (read)
{

// read input message
message = Console.ReadLine();
//
// quit console application
if (stringComparer.Equals("quit", message))
read = false;
//
// sleep for 10ms - frees resources
Thread.Sleep(10);
}
//
// kill the read thread
if (readThread != null)
readThread.Join();
readThread = null;
//
// destroy metar com object instance
if (metarCom != null)
{
metarCom.Dispose();
metarCom.Close();
}
metarCom = null;
}

However, if I kill the process from an external program, or close the CMD
instance that it it running in, it leaves part of the application running.

My question is - How do I test for these other methods of quitting the
application?

Thanks
 
A

Andrew

Thank you very much for your post!

I did the following:

readThread.IsBackground = true;

and I'm getting the same result as before.

Is there another thread that you are referring to?


--
Learning, always learning...


Chris Tacke said:
Set the thread to a background thread (assuming CF 2.0 or later).


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com

Andrew said:
I have a multi-threaded console application running in Windows CE using the
.NET Compact Framework 2.0.

The following code exits the program and correctly terminates the threads
created.

static void Main(string[] args)
{
string message;
StringComparer stringComparer =
StringComparer.OrdinalIgnoreCase;
//
// create metar serial port instance
metarCom = new SerialPort();
metarCom.ReadTimeout = 500;
//
// create task scheduler instance
taskScheduler = new ScheduledTimer();
taskScheduler.SchedulerElapsed += new
SchedulerElapsedEventHandler(taskScheduler_SchedulerElapsed);
//
// create settings instance
settings = new ManagerSettings(AppPath +
"\\AgentSettings.xml");
//
// initialize settings
InitializeSettings();

//
// open metar serial port
if(metarEnabled)
metarCom.Open();
//
// create thread instance to read
// serial port
readThread = new Thread(Read);
read = true;
if(metarEnabled)
readThread.Start();
//
//
// start scheduler
taskScheduler.Start();

//
Console.WriteLine("Type QUIT to exit");
//
// loop until quit message entered
while (read)
{

// read input message
message = Console.ReadLine();
//
// quit console application
if (stringComparer.Equals("quit", message))
read = false;
//
// sleep for 10ms - frees resources
Thread.Sleep(10);
}
//
// kill the read thread
if (readThread != null)
readThread.Join();
readThread = null;
//
// destroy metar com object instance
if (metarCom != null)
{
metarCom.Dispose();
metarCom.Close();
}
metarCom = null;
}

However, if I kill the process from an external program, or close the CMD
instance that it it running in, it leaves part of the application running.

My question is - How do I test for these other methods of quitting the
application?

Thanks
 
C

Chris Tacke, eMVP

If it's still not exiting my guess is that your Read() method (which I don't
se) is blocking somewhere. You need to have it periodically unblock to
check for exit conditions.

-Chris


Andrew said:
Thank you very much for your post!

I did the following:

readThread.IsBackground = true;

and I'm getting the same result as before.

Is there another thread that you are referring to?


--
Learning, always learning...


Chris Tacke said:
Set the thread to a background thread (assuming CF 2.0 or later).


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com

Andrew said:
I have a multi-threaded console application running in Windows CE using
the
.NET Compact Framework 2.0.

The following code exits the program and correctly terminates the
threads
created.

static void Main(string[] args)
{
string message;
StringComparer stringComparer =
StringComparer.OrdinalIgnoreCase;
//
// create metar serial port instance
metarCom = new SerialPort();
metarCom.ReadTimeout = 500;
//
// create task scheduler instance
taskScheduler = new ScheduledTimer();
taskScheduler.SchedulerElapsed += new
SchedulerElapsedEventHandler(taskScheduler_SchedulerElapsed);
//
// create settings instance
settings = new ManagerSettings(AppPath +
"\\AgentSettings.xml");
//
// initialize settings
InitializeSettings();

//
// open metar serial port
if(metarEnabled)
metarCom.Open();
//
// create thread instance to read
// serial port
readThread = new Thread(Read);
read = true;
if(metarEnabled)
readThread.Start();
//
//
// start scheduler
taskScheduler.Start();

//
Console.WriteLine("Type QUIT to exit");
//
// loop until quit message entered
while (read)
{

// read input message
message = Console.ReadLine();
//
// quit console application
if (stringComparer.Equals("quit", message))
read = false;
//
// sleep for 10ms - frees resources
Thread.Sleep(10);
}
//
// kill the read thread
if (readThread != null)
readThread.Join();
readThread = null;
//
// destroy metar com object instance
if (metarCom != null)
{
metarCom.Dispose();
metarCom.Close();
}
metarCom = null;
}

However, if I kill the process from an external program, or close the
CMD
instance that it it running in, it leaves part of the application
running.

My question is - How do I test for these other methods of quitting the
application?

Thanks
 
A

Andrew

My Read() method:

#region Read - Serial Ports
//
static void Read()
{
//
while (read)
{
if (mateCom.BytesToRead > 0)
try
{
// read data from buffer
mate = mateCom.ReadLine();
}
catch (TimeoutException ex)
{
Debug.WriteLine("Mate Serial Port Timeout");
// write to log file
ManagerSettings.WriteLog(AppLogPath,
"Program:Read()", "TimeoutException:" + ex.ToString());
}
//
if (pwrCom.BytesToRead > 0)
try
{
// read data from buffer
power = pwrCom.ReadLine();
}
catch (TimeoutException ex)
{
Debug.WriteLine("Power Meter Serial Port Timeout");
// write to log file
ManagerSettings.WriteLog(AppLogPath,
"Program:Read()", "TimeoutException:" + ex.ToString());
}
//
// sleep for 10ms - thread blocking
Thread.Sleep(10);
}
}

Is the sleep doing this?

Andrew
--
Learning, always learning...


Chris Tacke said:
If it's still not exiting my guess is that your Read() method (which I don't
se) is blocking somewhere. You need to have it periodically unblock to
check for exit conditions.

-Chris


Andrew said:
Thank you very much for your post!

I did the following:

readThread.IsBackground = true;

and I'm getting the same result as before.

Is there another thread that you are referring to?


--
Learning, always learning...


Chris Tacke said:
Set the thread to a background thread (assuming CF 2.0 or later).


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com

I have a multi-threaded console application running in Windows CE using
the
.NET Compact Framework 2.0.

The following code exits the program and correctly terminates the
threads
created.

static void Main(string[] args)
{
string message;
StringComparer stringComparer =
StringComparer.OrdinalIgnoreCase;
//
// create metar serial port instance
metarCom = new SerialPort();
metarCom.ReadTimeout = 500;
//
// create task scheduler instance
taskScheduler = new ScheduledTimer();
taskScheduler.SchedulerElapsed += new
SchedulerElapsedEventHandler(taskScheduler_SchedulerElapsed);
//
// create settings instance
settings = new ManagerSettings(AppPath +
"\\AgentSettings.xml");
//
// initialize settings
InitializeSettings();

//
// open metar serial port
if(metarEnabled)
metarCom.Open();
//
// create thread instance to read
// serial port
readThread = new Thread(Read);
read = true;
if(metarEnabled)
readThread.Start();
//
//
// start scheduler
taskScheduler.Start();

//
Console.WriteLine("Type QUIT to exit");
//
// loop until quit message entered
while (read)
{

// read input message
message = Console.ReadLine();
//
// quit console application
if (stringComparer.Equals("quit", message))
read = false;
//
// sleep for 10ms - frees resources
Thread.Sleep(10);
}
//
// kill the read thread
if (readThread != null)
readThread.Join();
readThread = null;
//
// destroy metar com object instance
if (metarCom != null)
{
metarCom.Dispose();
metarCom.Close();
}
metarCom = null;
}

However, if I kill the process from an external program, or close the
CMD
instance that it it running in, it leaves part of the application
running.

My question is - How do I test for these other methods of quitting the
application?

Thanks
 
C

Chris Tacke, eMVP

I'd have to verify by running code, but my bet is that ReadLine is a
blocking call. It likely stays blocked there until the port is closed (or
data is received). If you send data in after closing the app (which would
cause the thread to unblock and subsequently die) does the app fully exit
then?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com


Andrew said:
My Read() method:

#region Read - Serial Ports
//
static void Read()
{
//
while (read)
{
if (mateCom.BytesToRead > 0)
try
{
// read data from buffer
mate = mateCom.ReadLine();
}
catch (TimeoutException ex)
{
Debug.WriteLine("Mate Serial Port Timeout");
// write to log file
ManagerSettings.WriteLog(AppLogPath,
"Program:Read()", "TimeoutException:" + ex.ToString());
}
//
if (pwrCom.BytesToRead > 0)
try
{
// read data from buffer
power = pwrCom.ReadLine();
}
catch (TimeoutException ex)
{
Debug.WriteLine("Power Meter Serial Port Timeout");
// write to log file
ManagerSettings.WriteLog(AppLogPath,
"Program:Read()", "TimeoutException:" + ex.ToString());
}
//
// sleep for 10ms - thread blocking
Thread.Sleep(10);
}
}

Is the sleep doing this?

Andrew
--
Learning, always learning...


Chris Tacke said:
If it's still not exiting my guess is that your Read() method (which I
don't
se) is blocking somewhere. You need to have it periodically unblock to
check for exit conditions.

-Chris


Andrew said:
Thank you very much for your post!

I did the following:

readThread.IsBackground = true;

and I'm getting the same result as before.

Is there another thread that you are referring to?


--
Learning, always learning...


:

Set the thread to a background thread (assuming CF 2.0 or later).


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com

I have a multi-threaded console application running in Windows CE
using
the
.NET Compact Framework 2.0.

The following code exits the program and correctly terminates the
threads
created.

static void Main(string[] args)
{
string message;
StringComparer stringComparer =
StringComparer.OrdinalIgnoreCase;
//
// create metar serial port instance
metarCom = new SerialPort();
metarCom.ReadTimeout = 500;
//
// create task scheduler instance
taskScheduler = new ScheduledTimer();
taskScheduler.SchedulerElapsed += new
SchedulerElapsedEventHandler(taskScheduler_SchedulerElapsed);
//
// create settings instance
settings = new ManagerSettings(AppPath +
"\\AgentSettings.xml");
//
// initialize settings
InitializeSettings();

//
// open metar serial port
if(metarEnabled)
metarCom.Open();
//
// create thread instance to read
// serial port
readThread = new Thread(Read);
read = true;
if(metarEnabled)
readThread.Start();
//
//
// start scheduler
taskScheduler.Start();

//
Console.WriteLine("Type QUIT to exit");
//
// loop until quit message entered
while (read)
{

// read input message
message = Console.ReadLine();
//
// quit console application
if (stringComparer.Equals("quit", message))
read = false;
//
// sleep for 10ms - frees resources
Thread.Sleep(10);
}
//
// kill the read thread
if (readThread != null)
readThread.Join();
readThread = null;
//
// destroy metar com object instance
if (metarCom != null)
{
metarCom.Dispose();
metarCom.Close();
}
metarCom = null;
}

However, if I kill the process from an external program, or close
the
CMD
instance that it it running in, it leaves part of the application
running.

My question is - How do I test for these other methods of quitting
the
application?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top