Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Error when closing program during sleep

Hello,

I have written a temperature sensing program and during operation i have designed it to use a "non blocking" sleep for 5 seconds. The problem is when i close it by pushing the X in the top right corner of the window, it says application error - The address xxxxxxx at referenced memory xxxxxx could not be written.

This happens only when i try to exit during a sleep.

Does anybody know of how i can get around this?

Thanks
[465 byte] By [ifko] at [2007-11-11 6:28:42]
# 1 Re: Error when closing program during sleep
Just to give you people a bit of an update and more info.

I was using "End" before when either the person pushed exit or it detected a query unloadmode of 0 (when the person pushes the X in the corner) thats when it gave me memory could not be written error.

I removed the "End" and used an "Unload Me" and now if the program isn't sleeping the unload me will work fine, but if the program is sleeping then the program will close without any errors but then a second later the program will open itself up again.

Should i be using anything else to close the program?
ifko at 2007-11-11 17:28:23 >
# 2 Re: Error when closing program during sleep
can you explain what you mean with "non-blocking sleep?"
Marco
mstraf at 2007-11-11 17:29:23 >
# 3 Re: Error when closing program during sleep
sorry,

by "non-blocking sleep" i mean that the sleep does not interfere with the users interaction of the program (i can still minimize the program, push buttons, click on the 'File' menu).

The regular sleep makes it seem that the program is non-reponsive.
ifko at 2007-11-11 17:30:32 >
# 4 Re: Error when closing program during sleep
here's a link to a page where i got the code for the sleep function.

http://www.smsoft.ru/en/vbwait.htm

I was wondering if it was possible to terminate the sleep function when i exit the program so that it can close normally.
ifko at 2007-11-11 17:31:29 >
# 5 Re: Error when closing program during sleep
There are few ways to do it.

In the code of the link you posted, the problem is in handling the MsgWaitForMultipleObjects API. When the API returns with a list of events, instead of continuing the loop someone has to peek the events, and if one of them is WM_CLOSE (or something similar), the loop must be exited right away. And not only that: all the code AFTER that must be discarded.
It is a messy job, this is why I am strongly against these type ot API and the DoEvents method.

The cleaner solution in cases like this is a all VB solution: using a Timer control.
Just activate the Timer, and just forget about it. In the Timer event, do what you need to do. When the application exits, just stop the time (and sometimes is not even necessary). If you need to abort the operation, just add a button that disables the timer.

Marco
mstraf at 2007-11-11 17:32:28 >
# 6 Re: Error when closing program during sleep
alright so i did some fiddling with my program using what you had suggested, with a bit of a modification.

My program receives data from the serial port from a microcontroller, and my program doesn't control when data will come in it just uses the mscomm oncomm event to fire when i have the correct amount of bytes are received then it processes the data. So i can't run the entire program from the timer because the timing of the program wouldn't match with when the microcontroller sends the data (and i didn't want it piling up in the buffer).

This is a temperature monitoring program and the microcontroller get temperatures from all the sensors every 5 seconds during this time it is always checking to see if the RTS (Ready To Send) pin is 5 volts. My idea was to drive that pin low to 0 volts then sleep for a specified amount of time (a minute or so) then drive the pin high and do another cycle of temperature readings. This way i wouldn't be constantly polling all these sensors.

I tried calling a function which uses the VB timer to check if the wait time has elapsed using a do loop and do events. But it has the same error as before, the only way would be using the sleep function or the VB timer (without doevents), but either way the program seems frozen while it is sleeping but atleast it exits properly (after a delay while the sleep is ending).

Your idea seemed quite logical, assuming i understood it properly but once again i'm lost

Thanks for your input i appreciate it.
ifko at 2007-11-11 17:33:26 >
# 7 Re: Error when closing program during sleep
Why don't you try to solve your problem in a completely different way: don't allow the form to close during the sleep. Set up a boolean variable, eg Sleeping, and set it to true right before you sleep and back to false afterwards. In the Form QueryUnload event, add the following line of code:
If Sleeping Then Cancel = 1
Thus, when you press the X in the top right corner, nothing will happen.
yhilewitz at 2007-11-11 17:34:30 >
# 8 Re: Error when closing program during sleep
I do not understand: if you are reading the data from an event, why the need of a timer for pooling?
Marco
mstraf at 2007-11-11 17:35:30 >
# 9 Re: Error when closing program during sleep
Thanks for the help guys, but i'm going to remove the sleep entirely it's causing too many problems for it's worth
ifko at 2007-11-11 17:36:38 >
# 10 Re: Error when closing program during sleep
wise decision...
Marco
mstraf at 2007-11-11 17:37:38 >