Hmmm,
having had a look at the code there are a couple of places where the cancellation wasn't handled correctly in some cases, meaning that you could either get an error showing (rather than a cancelled message) or the frame actually captured in spite of being cancelled, depending on timings. I will fix both of those for the next update.
There is a more interesting issue though with the use of the Async methods from scripting... The async methods often involve work being done on more than one thread in SharpCap - as the work is completed the notification comes back to the original thread that kicked off the async operation. That's an issue for scripting, because the starting thread is going to be busy if the script is still running - unable to receive that notification, so from the point of view of scripting, the operation may seem to never complete or never cancel. It will tend to work from the interactive portion of the console, since while that is waiting for you to type, it can receive the notifications, but not for running a script.
I'm reluctant to try to work around this in the SharpCap code, as it would be quite intrusive and run the risk of breaking things for the 99.9% of people who are not using scripting. Instead, there is a workaround to use inside a script - run a function called 'Application.DoEvents' at any point where you are waiting for an async operation to complete. A simple example is as follows
Code: Select all
clr.AddReference("System.Threading")
clr.AddReference("System.Windows.Forms")
from System.Threading import CancellationTokenSource
from System.Windows.Forms import Application
from time import sleep
# A cancellation token source lets you cancel operations - you pass the token to the thing you might want to cancel and call 'Cancel' on the source
cts = CancellationTokenSource()
# CapturingSingleFrame is the property to check for single frame capture in progress - not Capturing
print (SharpCap.SelectedCamera.CapturingSingleFrame)
# Pass the cancellation token, and 'False' as the last parameter ensures that the next frame to arrive is saved, not the frame currently being viewed
task = SharpCap.SelectedCamera.CaptureSingleFrameToAsync("f:\\bbb.png", cts.Token, False)
# Will now report True
print (SharpCap.SelectedCamera.CapturingSingleFrame)
sleep(2)
Application.DoEvents();
# Request cancellation via the token source
cts.Cancel()
# Without this DoEvents, the cancellation does not complete correctly or update the status of CapturingSingleFrame
Application.DoEvents();
# Should now pring False
print (SharpCap.SelectedCamera.CapturingSingleFrame)
In general, if you are waiting for something to async to complete, run a mixture of Application.DoEvents() and short 'sleep' statements. You can either look at SharpCap properties (like CapturingSingleFrame) as shown above, or look at the Status of the task returned by the async operation - that will go through the stages shown here (
https://learn.microsoft.com/en-us/dotne ... ew=net-8.0) to end up in RanToCompletion/Cancelled/Faulted
cheers,
Robin