T
TC
I'm confused about the proper way to shut down my application.
I have a form which instantiates a control, which instantiates another
control, which instantiates yet another control that runs a time-
consuming loop. I put Application.DoEvents() into the loop so the user
can continue to work on the form while the loop is running. Everything
works great until the user wants to exit the application. The
application will not close until the loop is done. That's bad. I need
to interrupt the loop and close immediately when the user wants to
exit.
I've figured out that I can respond to the FormClosing event on the
form, then call a procedure in the child control which calls a
procedure in the grandchild control which calls a procedure in the
great-grandchild control that sets a flag and stops the loop.
My question is this: Do I really have to write a set of cascading
procedures to shut down the application like this, or is there a more
elegant way? It seems to me that the application must "know" it is
waiting for my loop to finish. How can I use this awareness from
within my code? Is there some event I can respond to, or some
application variable I can check?
I used to think Finalize existed for this purpose -- I thought it's
job was to announce a pending shutdown to every instance of a class/
control. However, I've learned that Finalize doesn't get called until
after the loop is already done, so it obviously doesn't help in this
case.
By the way, I've browsed this newsgroup and I've seen the suggestions
that starting a new thread is preferable to DoEvents. I'm working on
that, but I have a lot of cross-threading problems to resolve first.
In any case, I believe that is a separate issue. Even if I run my loop
on a separate thread, instead of using DoEvents, I still need to
figure out how to shut it down, right?
-TC
I have a form which instantiates a control, which instantiates another
control, which instantiates yet another control that runs a time-
consuming loop. I put Application.DoEvents() into the loop so the user
can continue to work on the form while the loop is running. Everything
works great until the user wants to exit the application. The
application will not close until the loop is done. That's bad. I need
to interrupt the loop and close immediately when the user wants to
exit.
I've figured out that I can respond to the FormClosing event on the
form, then call a procedure in the child control which calls a
procedure in the grandchild control which calls a procedure in the
great-grandchild control that sets a flag and stops the loop.
My question is this: Do I really have to write a set of cascading
procedures to shut down the application like this, or is there a more
elegant way? It seems to me that the application must "know" it is
waiting for my loop to finish. How can I use this awareness from
within my code? Is there some event I can respond to, or some
application variable I can check?
I used to think Finalize existed for this purpose -- I thought it's
job was to announce a pending shutdown to every instance of a class/
control. However, I've learned that Finalize doesn't get called until
after the loop is already done, so it obviously doesn't help in this
case.
By the way, I've browsed this newsgroup and I've seen the suggestions
that starting a new thread is preferable to DoEvents. I'm working on
that, but I have a lot of cross-threading problems to resolve first.
In any case, I believe that is a separate issue. Even if I run my loop
on a separate thread, instead of using DoEvents, I still need to
figure out how to shut it down, right?
-TC