How to wait for the current lightning creation process to finish when pressing the cancel button?
The logic: When clicking the btnStartProcessing_Click it will start processing next click will stop processing. if clicking the button to stop the processing and it's in the middle of processing a lightning then it will wait for the current lightning processing to finish and then will end the overall process.
This is working fine when clicking the button in the middle of processing the first lightning or the third lightning fourth fifth sixth any lightning except the second one.
I can tell why when I click the cancel button and it's processing the second lightning and it's only the second one then when it's finishing processing the second lightning instead stop and cancel all the operation it keeps going on to the next lightnings.
The problem is only with the second lightning process.
The code is a bit long, but everything is connected.
The top of form1:
The btnStartProcessing_Click event:
The ProcessingLightning method:
and the ProcessVideoFramesAsync method: