|
|
Home » Community » Newbie corner » Shutdown procedure by clicking red x
Shutdown procedure by clicking red x [message #30002] |
Thu, 02 December 2010 15:17  |
nlneilson
Messages: 644 Registered: January 2010 Location: U.S. California. Mojave &...
|
Contributor |
|
|
I have an app created with Upp that works great most of the time.
I am wondering about the shutdown procedure when clicking on the red x to end the app. The code has:
void Exit0() {
if(CommPort.IsOpened()) CommPort.Close();
if(CommPort2.IsOpened()) CommPort2.Close();
Thread::ShutdownThreads();
Break(); // Closes window with 'Cancel'
}
GUI_APP_MAIN{
GPSv2().Run();
if(CommPort.IsOpened()) CommPort.Close();
if(CommPort2.IsOpened()) CommPort2.Close();
Thread::ShutdownThreads();
}
When the app is closed with "void Exit0()" it explicitly closes any open ports and shuts down any threads.
When clicking the red x to end the app will this close the ports and end the threads if that is under _MAIN?
[Updated on: Thu, 02 December 2010 15:21] Report message to a moderator
|
|
|
Re: Shutdown procedure by clicking red x [message #30120 is a reply to message #30002] |
Thu, 09 December 2010 21:37   |
nlneilson
Messages: 644 Registered: January 2010 Location: U.S. California. Mojave &...
|
Contributor |
|
|
After searching found this that gets deep into the red X to close:
http://www.codeguru.com/columns/vb/article.php/c15579
Also a thread wait would not work as the threads will continuously run unless shut down, which was the problem that seldom happened that required taskmanager or the MS pop up to terminate the app.
With some semi intuitive reasoning to end the threads that have in a while loop with Sleep(980) added this:
// A global bool end;
while (CommPort.ReadDataWaiting() ) {
if(end) break;
...
Sleep(980);
....
void Exit0() {
end = true; Sleep(2000);
if(CommPort.IsOpened()) CommPort.Close();
if(CommPort2.IsOpened()) CommPort2.Close();
Thread::ShutdownThreads();
Break(); // Closes window with 'Cancel'
}
void endX(){end = true; Sleep(2000);}
GUI_APP_MAIN{
GPSv2().Run();
endX();
if(CommPort.IsOpened()) CommPort.Close();
if(CommPort2.IsOpened()) CommPort2.Close();
Thread::ShutdownThreads();
}
Setting end = true; and the Sleep(2000); gives the while loop time to break.
Sometimes if a GPS receiver is not closed properly pulling the receiver plug is about the only way.
This works when clicking the red x (or at least I have not had the error again) as well as the explicit Exit.
Neil
http://www.nlneilson.com/gpsv2.html
[Updated on: Fri, 10 December 2010 00:32] Report message to a moderator
|
|
|
Re: Shutdown procedure by clicking red x [message #30330 is a reply to message #30120] |
Fri, 24 December 2010 23:45   |
alendar
Messages: 47 Registered: January 2010 Location: Idaho, USA
|
Member |
|
|
Hi Neil,
I used this technique to interrupt a thread at closing time:
enum ListLoaderEnum {LL_RESET, LL_KILLLOAD};
class MainWin : public WithMainLayout<TopWindow> {
protected:
StaticMutex loadListLock;
Thread listLoaderThread;
volatile Atomic stopFetchingTags;
public:
MainWin() : stopFetchingTags(0) {
virtual void Close() {
// While we are in the Close event, no threads can continue, so a Wait will lock up the system
// Notify threads to stop, then trigger a callback
int x = listLoaderThread.GetCount();
if (x) {
loadListLock.Enter();
stopFetchingTags = LL_KILLLOAD;
loadListLock.Leave();
this->ProcessEvents();
Sleep(100);
PostCallback(THISBACK(Close));
} else {
TopWindow::Close(); // Won't close unless this is called
}
}
void EnrichDbFromTags() {
listLoaderThread.Run(THISBACK(EnrichDbFromTagsThread));
}
void EnrichDbFromTagsThread() {
stopFetchingTags = LL_RESET;
...
while(lst.Fetch()) {
if (stopFetchingTags == LL_KILLLOAD) {
break;
}
...
}
}
I tried the Thread::IsShutdownThreads() but it just locked up tight. The StaticMutex is probably overkill.
Jeff
cd7651feeb698f6ac6cec1f6deda5e5b
|
|
|
Re: Shutdown procedure by clicking red x [message #30333 is a reply to message #30002] |
Sat, 25 December 2010 08:44   |
nlneilson
Messages: 644 Registered: January 2010 Location: U.S. California. Mojave &...
|
Contributor |
|
|
I had a few times when the app locked also on
Thread::ShutdownThreads(); or Thread::IsShutdownThreads()
I don't remember which but in debug just jumped from there and then stopped.
In a release it just hung and MS closed it.
This is what I changed to:
while (CommPort.ReadDataWaiting() ) {
if(end) break;
try{
....
void endX(){end = true; Sleep(2000);}
GUI_APP_MAIN{
GPSx2().Run();
if(CommPort.IsOpened()) CommPort.Close();
if(CommPort2.IsOpened()) CommPort2.Close();
endX();
}
This is in a GPS tracking app that has Sleep(1000) waiting for the next sentence, if not it checks every (500) to see if the signal is regained.
The if(end) break; closes the thread with the break inside the thread.
bool end; and the void endX() are global (if that is the right term).
I did it about the same way in a TrackReplay app, triggering a break inside the thread to end it.
Maybe a hack but no problem closing since.
Your code is above me just looking at it, will study it later.
It does look like you have a break inside the thread to close it also.
StaticMutex, volatile Atomic, PostCallback, are all new to me.
Neil
[Updated on: Sat, 25 December 2010 09:17] Report message to a moderator
|
|
|
Re: Shutdown procedure by clicking red x [message #30374 is a reply to message #30333] |
Mon, 27 December 2010 04:13   |
nlneilson
Messages: 644 Registered: January 2010 Location: U.S. California. Mojave &...
|
Contributor |
|
|
mutex ~ mutual exclusion
No variables are shared or assessable between the two threads so that is not an issue.
I see you have StaticMutex but only one thread other than the main thread, I guess they could be both using the same variable at the same time.
PostCallBack ~ this thread gets into it but as mentioned above the data is not shared between the two worker threads
http://www.ultimatepp.org/forum/index.php?t=msg&goto=105 45&
I did look at this:
"I am not expert in threads, but it seems you use gui inside your threads. It would be better to be in main program."
http://www.ultimatepp.org/forum/index.php?t=msg&goto=298 57&
The two threads each use data from seperate GPS receivers and modify the content of seperate EditFields. The main thread can modify the format it is displayed in.
Sigq<<=Sigq;
Location<<=Lat + "," + Lon + "," + fAlt;
This works OK changing the data in the threads rather than in main.
atomic ~ concurrent reads and writes ~ a thread in my case does not read/write the other threads variables.
note: As far as error handling after about an hour the count for each GPS receiver may vary, I don't use the checksum for each sentence but as long as the Lat, Lon and Alt are OK it is used, otherwise it is skipped. It will not exit the app if a sentence is bad/skipped or missing.
Neil
[Updated on: Mon, 27 December 2010 04:47] Report message to a moderator
|
|
|
|
|
|
|
Goto Forum:
Current Time: Sun Apr 27 12:45:59 CEST 2025
Total time taken to generate the page: 0.00725 seconds
|
|
|