subtle issue with Application.DoEvents() behavior

  • Thread starter Thread starter Wiktor Zychla
  • Start date Start date
W

Wiktor Zychla

Hello there,

I have a single threaded app that sometimes performs lenghty operations.
When such operation is performed, a splash form is shown with "Abort lenghty
operation" button. As you can expect, the button event is handled indirectly
via Application.DoEvents() that is put inside the lenghty operation loop (in
any other case the button event would not be raised in the middle of a
lenghty loop).

and now comes a problem. Application.DoEvents() handles all messages
that are stored in a message queue. when user closes the main application
window, his request is ALSO handled! the application closes in the middle of
an operation!

I can, of course, put a global flag to indicate the lenghty operation
and in the Closing event of a main window check this flag but I would like
to find another, more elegant solution.

I thought that it could be possible to look at the current stack trace
and check if there are stack frames (at the top of the stack trace) other
than the one that caused the main form to show up. if that would be the
case, I could conclude that some operation currently is performed and the
main form should not be closed. the user could be informed with "an
operation is in progress, so the application cannot be closed".

my problem is as follows: I have no idea what BCL method could be used
to check the current stack trace. I know that the stack trace can be checked
inside catch(...) clausule but this is not my case, I would like to check it
in normal code. how do I do that?

or maybe there are other ellegant solutions to my issue?

thanks in advance,
Wiktor Zychla
 
Wiktor,

I think that you are going about this the wrong way. Basically, you are
looking for a solution to get around using another thread, and it seems that
the solution is more complex than the solution that I feel you should be
using.

If you place everything in another thread, you don't need DoEvents, and
you can handle shut downs of your app properly (have a shared variable that
is locked on and during each iteration of your loop, check the variable to
see if you should continue).

Hope this helps.
 
Back
Top