Can Anyone See The Bug Here?

Discussions on extending SharpCap using the built in Python scripting functionality
Post Reply
User avatar
1CM69
Posts: 62
Joined: Tue Nov 14, 2017 9:49 pm

Can Anyone See The Bug Here?

Post by 1CM69 » Fri Aug 24, 2018 9:14 pm

Hi,

I've spent most of the day debugging my script but to no avail.

Lots of errors have been rectified, mostly pertaining to INDENTS but still I cannot see what else I am missing.

It's really strange because I have a working script version but now I have built a GUI around this script, it is failing.

Here's my latest unworking script with GUI:
GUI_2.txt
(22.44 KiB) Downloaded 52 times
Here's the working version without GUI:
LRGB_Sequence_Capture.txt
(4.38 KiB) Downloaded 48 times
Any pointers would be lovely.

Forgot to mention that the script runs fine but then hangs right after this line:

Code: Select all

self.appendMessageText('Capturing LUMINANCE Channel: (' + str(count) + '/' + str(capSeq) + ')')
SharpCap.Wheels.SelectedWheel.Position = 1
Cheers..,

Kirk

User avatar
oopfan
Posts: 456
Joined: Sat Jul 08, 2017 2:37 pm
Location: New York
Contact:

Re: Can Anyone See The Bug Here?

Post by oopfan » Fri Aug 24, 2018 10:02 pm

Code: Select all

while True:
	if not SharpCap.SelectedCamera.Capturing :
		break
time.sleep(0.5)
If you ask me, that 'time.sleep' should be inside the while-loop. The way you have it written that while-loop is bogarting the CPU. By putting the 'time.sleep' inside the while-loop you are giving the capture time to execute.

That was it. In your working code you have the indentation right. This is one thing that I dislike about Python -- indentations.

Brian

User avatar
1CM69
Posts: 62
Joined: Tue Nov 14, 2017 9:49 pm

Re: Can Anyone See The Bug Here?

Post by 1CM69 » Fri Aug 24, 2018 11:11 pm

oopfan wrote:
Fri Aug 24, 2018 10:02 pm

Code: Select all

while True:
	if not SharpCap.SelectedCamera.Capturing :
		break
time.sleep(0.5)
If you ask me, that 'time.sleep' should be inside the while-loop. The way you have it written that while-loop is bogarting the CPU. By putting the 'time.sleep' inside the while-loop you are giving the capture time to execute.

That was it. In your working code you have the indentation right. This is one thing that I dislike about Python -- indentations.

Brian
OK, thanks Brian.

I’ll have a look at that in the morning.

Yes, indentation, so very easy to get lost in whitespace lol

Cheers..,

Kitk

User avatar
1CM69
Posts: 62
Joined: Tue Nov 14, 2017 9:49 pm

Re: Can Anyone See The Bug Here?

Post by 1CM69 » Mon Aug 27, 2018 5:34 pm

Well I am stumped... :?

I definitely thought that it was down to indents causing the issue but I have seemingly tried every combination under the sun with no joy.

The script works perfectly as a bare script with no GUI but as soon as I wrap the code inside a Button Click it hangs.

By trial and error and the insertion of a 'MessageBox.Show' at various places along the code I have narrowed it down to the 'While True:' loop

So upon clicking my GUI Button the first part of the function performs as planned, moving the filterwheel to position1 if not already there.

It displays the message that it's capturing LUMINANCE channel and starts to capture.

Here is my first indication of failure because when the working bare script reaches this point SharpCap shows the capture countdown as per normal in the right hand side of the statusbar and the 'Stop capture' button on the toolbar is enabled.

On my GUI script, neither the countdown is displayed or the the 'Stop capture' button enabled.

So like I have stated, by careful placement of a 'MessageBox.Show' directly after the first 'While.True:' I can see that the code never gets past this point and constantly rewrites the 'MessageBox' to the screen, therefore for some reason the 'While True:' is not stepping in to the 'if not SharpCap.SelectedCamera.Capturing :' line and breaking the loop when the 'CaptureLimitValue' is reached.

Here are the section of code, first the working code:
Working Capture Code.txt
(2.8 KiB) Downloaded 49 times
and the non working code:
Non Working Capture Code.txt
(4.1 KiB) Downloaded 53 times
Regards..,

Kirk

User avatar
oopfan
Posts: 456
Joined: Sat Jul 08, 2017 2:37 pm
Location: New York
Contact:

Re: Can Anyone See The Bug Here?

Post by oopfan » Mon Aug 27, 2018 7:17 pm

Kirk,

I can't say if this will fix your problem but if you go back to one of your original posts where I attached my Python code:

viewtopic.php?f=14&t=1029

You will see that I spawn a worker thread for operations that take a long time to execute. In GUI programming you don't want to kick off a lengthy procedure from the UI thread, especially if that procedure also wants to update the UI. This is true for all languages not just Python.

Here are the important bits:

Code: Select all

class MyForm(Form):
	def onClickButton(self, sender, event):
		worker = WorkerThread(self)
		delegate = ThreadStart(worker.doWork)
		thread = Thread(delegate)
		thread.Start()

class WorkerThread:
	def __init__(self, form):
		self.form = form

	def doWork(self):
		self.form.appendMessageText("Working...")
		my_function(self.form);
		self.form.appendMessageText("Done.")
"my_function" is in global scope in my example. If you want you can inline the code in the doWork method but it is not good practice.

Brian

User avatar
1CM69
Posts: 62
Joined: Tue Nov 14, 2017 9:49 pm

Re: Can Anyone See The Bug Here?

Post by 1CM69 » Mon Aug 27, 2018 8:53 pm

oopfan wrote:
Mon Aug 27, 2018 7:17 pm
Kirk,

I can't say if this will fix your problem but if you go back to one of your original posts where I attached my Python code:

viewtopic.php?f=14&t=1029

You will see that I spawn a worker thread for operations that take a long time to execute. In GUI programming you don't want to kick off a lengthy procedure from the UI thread, especially if that procedure also wants to update the UI. This is true for all languages not just Python.

Here are the important bits:

Code: Select all

class MyForm(Form):
	def onClickButton(self, sender, event):
		worker = WorkerThread(self)
		delegate = ThreadStart(worker.doWork)
		thread = Thread(delegate)
		thread.Start()

class WorkerThread:
	def __init__(self, form):
		self.form = form

	def doWork(self):
		self.form.appendMessageText("Working...")
		my_function(self.form);
		self.form.appendMessageText("Done.")
"my_function" is in global scope in my example. If you want you can inline the code in the doWork method but it is not good practice.

Brian
Thanks Brian,
I was just looking through your earlier code too.

I had to change this line:

Code: Select all

my_function(self.form);
to:

Code: Select all

self.form.my_function()
and all is now working.

Just need to finish off a little and tidy up my script.

Thanks for all the help.

Regards..,

Kirk

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest