Question Code still running after Close()

Maylar

Member
Joined
Jan 22, 2021
Messages
18
Programming Experience
10+
I want to create an asychronus Quit method within my form. Basically, an immediate abort of all processes and close the form. I have a button that calls Close(), but the code that was running when the button was pressed keeps running, and now objects in the form no longer exist and an exception is thrown.

Is there a way to terminate any running method before calling Close()?

Or, is there an elegant way of doing this?

TIA
 
Last edited:
Environment.Exit(integer_here) is the inelegant way.

Elegant is to use the CancellationToken facility whereby your async process should regularly check whether it's being requested to cancel

I'm afraid your requirement of "instantly elegantly quit" is nearly impossible to meet; it's either instant, or it's elegant; really hard to achieve both
 
As noted above, you can't do immediate, but you can do as a soon as your background threads check on whether to stop or not.

At some point you're likely to stumble across how to kill a thread. Do not use that approach.
 
Back
Top Bottom