Scripting Issue post Sharpcap v9094

A place to report problems and bugs in SharpCap
Forum rules


If you have a problem or question, please check the FAQ to see if it already has an answer : https://www.sharpcap.co.uk/sharpcap-faqs

Please also read about Troubleshooting USB Issues before posting.

*** Please do not post license keys - please report any problems with licensing to 'admin' by private message ***

Please include the following details in any bug report:

* Version of SharpCap
* Camera and other hardware being user
* Operating system version
* Contents of the SharpCap log after the problem has occurred.
[If SharpCap crashes, please send the bug report when prompted instead of including the log]
fly_n
Posts: 36
Joined: Wed Oct 02, 2019 7:54 pm

Scripting Issue post Sharpcap v9094

#1

Post by fly_n »

Hello Robin,

to align my mount, I rely on the "no polaris - no problem" script, as posted in this forum.
Updateing to the latest versions of Sharpcap I have noticed that the execution of the script causes Sharpcap to hang and subsequently crash.

To avoid the issue I have reverted to build 9094 of Sharpcap.
Could you please let me know if anything in the scripting part of Sharpcap was intentionally changed and point me in the direction of what changes I would need to make, or does this sound to you like an unanticipated behavior?

Versions of Sharpcap tested working:
8604, 8894, 9063, 9094

Versions of Sharpcap tested crashing (after launching script)
9132, 9208, 9224, 9226

Thank you for your insights,
Norman
User avatar
admin
Site Admin
Posts: 13177
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Scripting Issue post Sharpcap v9094

#2

Post by admin »

Hi Norman,

does the script use a custom button in the toolbar? I did make a change between those two versions that alters the way that scripting code from custom buttons is run (to make it get run in a way that is closer to the way script is run when using the console). Of course it could be that the 'no polaris no problem' script was designed for the old way of doing things and the change has been incompatible with something in the script.

Does it freeze straight away or does it do a few things then get to a certain point then freeze?

I can see from the code that it writes its own log (by the look of it to the desktop), so the last few lines in that before the freeze might help identify where the problem occurs, similarly the last few lines in the SharpCap log (show the SharpCap log before trying the script).

cheers,

Robin
fly_n
Posts: 36
Joined: Wed Oct 02, 2019 7:54 pm

Re: Scripting Issue post Sharpcap v9094

#3

Post by fly_n »

Hello Robin,

it freezes right after the script is launched through the icon.
It does not log anything into the log file while freezing - other than the system is not responding.

I've reduced the script to the Form with a button that does nothing.
This mini script works in 9094 but is sufficient to crash the versions thereafter.

Maybe you could point me into the direction of how a custom button should look like after the change.

Thank you in advance, Norman

-----------------------
import clr
import csv
import math
import os
import sys


clr.AddReference("SharpCap")
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')


from SharpCap.UI import CaptureLimitType
from System.Windows.Forms import *
from System.Drawing import Point, Color, Font, FontStyle, Size, Image, Icon


default_icon = "C:\\Program Files (x86)\\SharpCap 3.3\\icons\\NP2_ALIGN.ico"


#############################################################################################################################
### initializations
readyness = False




# definition of UI form
class Alignment(Form):
def __init__(self):
self.Text = "No Polaris, no problem - polar alignment"
self.TopMost = True
self.Width = 900
self.Height = 400
self.BackColor = Color.FromName("Black")
self.ForeColor = Color.FromName("Orange")

###################################################################################
### First row of buttons

# Button Check Readyness
self.ready = Button()
self.ready.Text = 'readyness'
self.ready.Width = 100
self.ready.Height = 28
self.ready.Location = Point(20, 190)
self.ready.Font = Font(self.ready.Font, FontStyle.Bold)




###################################################################################
### Controls
self.Controls.Add(self.ready)


self.processing = False
self.ResumeLayout(False)



def OnFormClosing(self, e, *args):
e.Cancel = True
form.Hide()


form = Alignment()
form.icon = Icon(default_icon)


def show_script():
form.Show()

SharpCap.AddCustomButton("POLAR ALIGN", Image.FromFile(default_icon), None, show_script)
User avatar
admin
Site Admin
Posts: 13177
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Scripting Issue post Sharpcap v9094

#4

Post by admin »

Hi,

OK, I think I know what is going on - because the button click is running on a different thread to before (and you have used form.Show() not form.ShowDialog()), the form cannot work properly because the thread in question does not have a message loop.

Yuck...

I will have to go back and look at the original problem that I was trying to solve and see if there is an alternate way around, since I'm not seeing an easy fix based on the current design :(

cheers,

Robin
fly_n
Posts: 36
Joined: Wed Oct 02, 2019 7:54 pm

Re: Scripting Issue post Sharpcap v9094

#5

Post by fly_n »

Hello Robin,
I modified the script to use:
form.ShowDialog()

I am now able to run the script, using the test mode of the script.
Don't know if any draw backs exist.
From what I read online the form.ShowDialog() should prevent changing focus compared to form.Show().... but I cannot see this behavior.
Using form.ShowDialog() I can minimize the script and change settings in the main window and go back to the script form as before.

What I want to say is that it seems to me, as if this small change could be enough to get the script working, although I do not understand any of the context.

Kind regards,
Norman
User avatar
admin
Site Admin
Posts: 13177
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Scripting Issue post Sharpcap v9094

#6

Post by admin »

Hi Norman,

yes, that's probably a valid workaround for the moment. Normally ShowDialog does two different things - first it locks the parent window of the form that you do ShowDialog on and second it runs its own message loop. In this case the message loop part fixes the issue and the fact that you have not set the parent of the form means that the main SharpCap window does not get locked.

I looked back at why I made the change that caused the issue in the first place - it was to try to get sequencer steps run from script run from a custom button to work properly. I think I have found an alternative way to do that, which means I can wind back the change that is breaking the no polaris no problem script.

This will be in Monday's update.

cheers,

Robin
fly_n
Posts: 36
Joined: Wed Oct 02, 2019 7:54 pm

Re: Scripting Issue post Sharpcap v9094

#7

Post by fly_n »

Hello Robin,

thank you for the update in version 9246.
Yesterday night I was out to test the new version and can confirm that the scripting issue I had is fixed.
I'll post an update of my script soonish, to have better alignment of the buttons with version 4.xxxx of Sharpcap.

Kind regards,
Norman
User avatar
admin
Site Admin
Posts: 13177
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Scripting Issue post Sharpcap v9094

#8

Post by admin »

Hi Norman,

glad that sorted the problem for you and thanks for the update :)

cheers,

Robin
Jean-Francois
Posts: 361
Joined: Sun Oct 13, 2019 10:52 am
Location: Germany

Re: Scripting Issue post Sharpcap v9094

#9

Post by Jean-Francois »

Hello Robin,

What are the difference between the following commands ? :

form.ShowDialog()
Application.Run(form)
form.Show()

I change my script for the SHG film capture in a "menu" version. Is one of the command better to use ?

In addition, I have a problem with the script window scaling (font size).
Here a capture of the script window, one from the 4K monitor/desktop and the other from the laptop (1920x1080).
Script_Laptop_Desktop.jpg
Script_Laptop_Desktop.jpg (60.27 KiB) Viewed 446 times

The laptop window shows all the information, but with more gap between the fields. The 4K window has missing information.
Note, that I have the same behaviour on the script from Norman ("no_polaris_no_problem").

Regards,
Jean-Francois
User avatar
admin
Site Admin
Posts: 13177
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Scripting Issue post Sharpcap v9094

#10

Post by admin »

Hi Jean-Francois,

Application.Run(form) creates a whole new Windows Forms application to run your form - probably excessive and best avoided.
form.ShowDialog() locks any parent windows of the form while it is active, so that you have to finish with the form before going back and doing other stuff. ShowDialog will return when the form closes.
form.Show() just shows the form - you can still interact with the parent windows. Show() will return once the form is visible you will need to separate code to handle the form closing and doing stuff when it closes.

As to the font sizing, SharpCap now runs in DPI aware mode, so it will scale automatically adjust to different monitor resolutions. However by default Windows assumes that everything in the SharpCap process is also DPI aware, so you get mess ups like the one you are seeing when you show UI from code that isn't DPI aware.

The easiest thing to do is probably to temporarily turn off DPI awareness while you create your form - code below to do that, which I am happy to share.

Robin

Code: Select all


   using (new DpiWarenessContextChanger())
   {
   	form.ShowDialog(); // or form.Show();
   }


  public class DpiAwarenessContextChanger : IDisposable
    {
        public static readonly IntPtr DPI_AWARENESS_CONTEXT_UNAWARE = new IntPtr(-1);
        public static readonly IntPtr DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = new IntPtr(-2);
        public static readonly IntPtr DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = new IntPtr(-3);
        private readonly IntPtr _oldContext;


        [DllImport("user32.dll")] public static extern IntPtr GetWindowDpiAwarenessContext(IntPtr hwnd);
        /// <summary>
        /// Set the DPI awareness for the current thread to the provided value.
        /// </summary>
        /// <param name="dpiContext">The new DPI_AWARENESS_CONTEXT for the current thread. This context includes the <see cref="DPI_AWARENESS"/> value.</param>
        /// <returns>The old DPI_AWARENESS_CONTEXT for the thread. If the <paramref name="dpiContext"/> is invalid, the thread will not be updated and the return value will be NULL. You can use this value to restore the old DPI_AWARENESS_CONTEXT after overriding it with a predefined value.</returns>
        [DllImport("user32.dll")] public static extern IntPtr SetThreadDpiAwarenessContext(IntPtr dpiContext);

        public DpiAwarenessContextChanger()
        {
            _oldContext = ChangeContextTo(DPI_AWARENESS_CONTEXT_UNAWARE);
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        IntPtr SetContext(IntPtr newContext)
        {
            return SetThreadDpiAwarenessContext(newContext);
        }

        private IntPtr ChangeContextTo(IntPtr dpiAwarenessContextUnaware)
        {
            try
            {
                return SetContext(dpiAwarenessContextUnaware);
            }
            catch (Exception )
            {
                return IntPtr.Zero;
                
            }
        }

        public void Dispose()
        {
            ChangeContextTo(_oldContext);
        }
    }

Post Reply