Jump to content

Close doesn't work


Trax
 Share

Recommended Posts

   I have been out of commission way to long. Making newbie mistakes. For some reason the Close (X in upper right) isn't exiting the program. No matter how many times I click on that X $nMsg comes back as -11 so the program never closes. What am I doing wrong?

 

    $nMsg = GUIGetMsg()
    ConsoleWrite($nMsg)
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

Link to comment
Share on other sites

  • Moderators

This is a basic template you can use, should show you why you're having the issue.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $hGUI = GuiCreate("Test", 300, 400)
GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd

 

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Do you do this in a loop?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

It is in a loop and this is hard to explain but if you look through  

GUIConstantsEx.au3
$GUI_EVENT_CLOSE is defined as -3. For some reason when I click on the X in the upper right, or anywhere on the screen for that fact, a value of -11 is returned GUIGetMsg() is returning a value of -11 so it never exits........ 

So to define the question I guess it would really be... does anyone have any idea why GUIGetMsg() is returning a value of -11 when the X is clicked?

Link to comment
Share on other sites

The example scripts exit as expected when clicking the X. Your script doesn't. So I guess the problem is related to your script.
Without seeing the whole script it is impossible to track down the issue.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

OK. The code isn't really all that long:


 

#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=SRT.ico
#AutoIt3Wrapper_Outfile=MeterReader.exe
#AutoIt3Wrapper_Res_Comment=
#AutoIt3Wrapper_Res_Description=
#AutoIt3Wrapper_Run_Au3Stripper=y
#Au3Stripper_Parameters=/so
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
AutoItSetOption("MouseCoordMode", 1) ; 0=Relative to Active Window, 1=Absolute Screen 2=Relitive to Client Area
;AutoItSetOption("MustDeclareVars", 1)
AutoItSetOption("WinTitleMatchMode", 2) ; match any substring in title
#include-once
Opt("TrayMenuMode", 1)

#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <CommInterface.au3>
#include <GuiListBox.au3>

$Form1_1 = GUICreate("Meter Readings", 531, 366, 239, 155)
$Label1 = GUICtrlCreateLabel("EF Deck Meters", 104, 24, 81, 17)
$Label2 = GUICtrlCreateLabel("Remelt Meters", 352, 24, 72, 17)
$Label3 = GUICtrlCreateLabel("Meter 1:", 48, 56, 43, 17)
$Label4 = GUICtrlCreateLabel("Meter 2:", 48, 96, 43, 17)
$Label5 = GUICtrlCreateLabel("Meter 3:", 48, 136, 43, 17)
$Label6 = GUICtrlCreateLabel("Meter 4:", 48, 176, 43, 17)
$Label7 = GUICtrlCreateLabel("Meter 5:", 48, 216, 43, 17)
$Label8 = GUICtrlCreateLabel("Meter 6:", 48, 256, 43, 17)
$Label9 = GUICtrlCreateLabel("ERMF Roof Temp:", 256, 56, 93, 17)
$Label10 = GUICtrlCreateLabel("ERMF Bath Temp:", 256, 96, 92, 17)
$Label11 = GUICtrlCreateLabel("Meter 9:", 304, 136, 43, 17)
$Label12 = GUICtrlCreateLabel("Meter 10:", 296, 176, 49, 17)
$Label13 = GUICtrlCreateLabel("Meter 11:", 296, 216, 49, 17)
$List1 = GUICtrlCreateList("", 96, 56, 89, 19)
$List2 = GUICtrlCreateList("", 96, 96, 89, 19)
$List3 = GUICtrlCreateList("", 96, 136, 89, 19)
$List4 = GUICtrlCreateList("", 96, 176, 89, 19)
$List5 = GUICtrlCreateList("", 96, 216, 89, 19)
$List6 = GUICtrlCreateList("", 96, 256, 89, 19)
$List7 = GUICtrlCreateList("", 352, 56, 89, 19)
$List8 = GUICtrlCreateList("", 352, 96, 89, 19)
$List9 = GUICtrlCreateList("", 352, 136, 89, 19)
$List10 = GUICtrlCreateList("", 352, 176, 89, 19)
$List11 = GUICtrlCreateList("", 352, 216, 89, 19)
$List12 = GUICtrlCreateList("", 48, 312, 441, 19)
GUISetState(@SW_SHOW)

Global $Baud = 9600, $Databits = 7, $Parity = "O", $Stopbits = 1
Global $ReadTimeout = 5000
Global $CommPort, $Meter, $Message, $Value
Global $SleepBetweenReads = 2000, $SleepAfterSend = 2000

While 1
    Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
    $CommPort = 35
    $Meter = 7
    ReadMeter()
    DisplayResults()
    Sleep($SleepBetweenReads)
    $CommPort = 35
    $Meter = 8
    ReadMeter()
    DisplayResults()
    Sleep($SleepBetweenReads)
WEnd

Func DisplayResults()
    Select
        Case $Meter = 1
            _GUICtrlListBox_DeleteString($List1, 0)
            _GUICtrlListBox_AddString($List1, $Value)
        Case $Meter = 2
            _GUICtrlListBox_DeleteString($List2, 0)
            _GUICtrlListBox_AddString($List2, $Value)
        Case $Meter = 3
            _GUICtrlListBox_DeleteString($List3, 0)
            _GUICtrlListBox_AddString($List3, $Value)
        Case $Meter = 4
            _GUICtrlListBox_DeleteString($List4, 0)
            _GUICtrlListBox_AddString($List4, $Value)
        Case $Meter = 5
            _GUICtrlListBox_DeleteString($List5, 0)
            _GUICtrlListBox_AddString($List5, $Value)
        Case $Meter = 6
            _GUICtrlListBox_DeleteString($List6, 0)
            _GUICtrlListBox_AddString($List6, $Value)
        Case $Meter = 7
            _GUICtrlListBox_DeleteString($List7, 0)
            _GUICtrlListBox_AddString($List7, $Value)
        Case $Meter = 8
            _GUICtrlListBox_DeleteString($List8, 0)
            _GUICtrlListBox_AddString($List8, $Value)
        Case $Meter = 9
            _GUICtrlListBox_DeleteString($List9, 0)
            _GUICtrlListBox_AddString($List9, $Value)
        Case $Meter = 10
            _GUICtrlListBox_DeleteString($List10, 0)
            _GUICtrlListBox_AddString($List10, $Value)
        Case $Meter = 11
            _GUICtrlListBox_DeleteString($List11, 0)
            _GUICtrlListBox_AddString($List11, $Value)
        Case Else ; If nothing matches then execute the following.
            DisplayMessage("Invalid Case Number")
    EndSelect
    DisplayMessage($Message)
EndFunc

Func DisplayMessage($StringToDisplay)
    _GUICtrlListBox_DeleteString($List12, 0)
    _GUICtrlListBox_AddString($List12, $StringToDisplay)
EndFunc

Func ReadMeter()
    Local $PortHandle, $ReceiveResult, $SendString
    Local $BytesToSend, $BytesSent
    $Message = ""
    DisplayMessage("Reading Meter " & $Meter)
    $PortHandle = _CommAPI_OpenCOMPort($Commport, $Baud, $Parity, $Databits, $StopBits)
    If $PortHandle = 0 Then
        $Value = "Error"
        $Message = "@error return code opening Meter " & $Meter & " was " & @error
        Return
        EndIf
    $SendString = "N" & $Meter & "TA$"
    $BytesToSend = StringLen($SendString)
    $BytesSent = _CommAPI_TransmitString($PortHandle, $SendString, 0)
    Sleep($SleepAfterSend)
    If ($BytesSent <> $BytesToSend) Then
        $Value = "Error"
        $Message = "@error return code sending " & $SendString & " was " & @error
        _CommAPI_ClosePort($PortHandle)
        Return
        EndIf
    $ReceiveResult = _CommAPI_ReceiveString($PortHandle, $ReadTimeout, 0, "", 0)
    _CommAPI_ClosePort($PortHandle)
    If $ReceiveResult = "" Then
        $Value = "Error"
        $Message = "@error return code receiving meter " & $Meter & " was " & @error
        Return
        EndIf
    $Value = StringRight($ReceiveResult, 10)
    $Value = StringStripWS($Value, 😎
EndFunc

 

Edited by JLogan3o13
Link to comment
Share on other sites

  • Moderators

The problem is your sleeps; you're calling so many that the script never has time to exit. If change this section:

While 1
    Switch GUIGetMsg()
       Case $GUI_EVENT_CLOSE
          Exit
    EndSwitch
    $CommPort = 35
    $Meter = 7
    ReadMeter()
    DisplayResults()
    Sleep($SleepBetweenReads)
    $CommPort = 35
    $Meter = 8
    ReadMeter()
    DisplayResults()
    Sleep($SleepBetweenReads)
WEnd

to just this:

While 1
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
       Exit
    EndSwitch
WEnd

You'll see that it now works. So you need to look at the multiple sleeps (each at 2 seconds long) that you have going on in that while loop

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Or you could use an alertable sleep like this :

_SleepEx($SleepBetweenReads,1)

Func _SleepEx($nMilliseconds, $bAlertable = 0)
    Local $aRet = DllCall('kernel32.dll', 'dword', 'SleepEx', 'dword', $nMilliseconds, 'bool', $bAlertable)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aRet[0]
EndFunc

But the GUI may take up to 4 seconds to respond, since it is stuck in the sleep functions.

Another approach is to use timers like this :

Local $hTimer = TimerInit(), $bFirst = False
While 1
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      Exit
  EndSwitch
  If TimerDiff($hTimer) >= $SleepBetweenReads Then
    $bFirst = Not $bFirst
    $CommPort = 35
    $Meter = $bFirst ? 7 : 8
    ConsoleWrite ($Meter & @CRLF)
;    ReadMeter()
;    DisplayResults()
    $hTimer = TimerInit()
  EndIf
WEnd

You do not need to set $CommPort repeatedly to the same value, but it is not an issue in itself.

Edited by Nine
Link to comment
Share on other sites

1 hour ago, Trax said:

I did a HotKeySet("{ESC}", "Quit") and it exits immediately

#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=SRT.ico
#AutoIt3Wrapper_Outfile=MeterReader.exe
#AutoIt3Wrapper_Res_Comment=
#AutoIt3Wrapper_Res_Description=
#AutoIt3Wrapper_Run_Au3Stripper=y
#Au3Stripper_Parameters=/so
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
AutoItSetOption("MouseCoordMode", 1) ; 0=Relative to Active Window, 1=Absolute Screen 2=Relitive to Client Area
;AutoItSetOption("MustDeclareVars", 1)
AutoItSetOption("WinTitleMatchMode", 2) ; match any substring in title
#include-once
Opt("TrayMenuMode", 1)

#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <CommInterface.au3>
#include <GuiListBox.au3>
#include <StringConstants.au3>

$Form1_1 = GUICreate("Meter Readings", 531, 366, 239, 155)
$Label1 = GUICtrlCreateLabel("EF Deck Meters", 104, 24, 81, 17)
$Label2 = GUICtrlCreateLabel("Remelt Meters", 352, 24, 72, 17)
$Label3 = GUICtrlCreateLabel("Meter 1:", 48, 56, 43, 17)
$Label4 = GUICtrlCreateLabel("Meter 2:", 48, 96, 43, 17)
$Label5 = GUICtrlCreateLabel("Meter 3:", 48, 136, 43, 17)
$Label6 = GUICtrlCreateLabel("Meter 4:", 48, 176, 43, 17)
$Label7 = GUICtrlCreateLabel("Meter 5:", 48, 216, 43, 17)
$Label8 = GUICtrlCreateLabel("Meter 6:", 48, 256, 43, 17)
$Label9 = GUICtrlCreateLabel("ERMF Roof Temp:", 256, 56, 93, 17)
$Label10 = GUICtrlCreateLabel("ERMF Bath Temp:", 256, 96, 92, 17)
$Label11 = GUICtrlCreateLabel("Meter 9:", 304, 136, 43, 17)
$Label12 = GUICtrlCreateLabel("Meter 10:", 296, 176, 49, 17)
$Label13 = GUICtrlCreateLabel("Meter 11:", 296, 216, 49, 17)
$List1 = GUICtrlCreateList("", 96, 56, 89, 19)
$List2 = GUICtrlCreateList("", 96, 96, 89, 19)
$List3 = GUICtrlCreateList("", 96, 136, 89, 19)
$List4 = GUICtrlCreateList("", 96, 176, 89, 19)
$List5 = GUICtrlCreateList("", 96, 216, 89, 19)
$List6 = GUICtrlCreateList("", 96, 256, 89, 19)
$List7 = GUICtrlCreateList("", 352, 56, 89, 19)
$List8 = GUICtrlCreateList("", 352, 96, 89, 19)
$List9 = GUICtrlCreateList("", 352, 136, 89, 19)
$List10 = GUICtrlCreateList("", 352, 176, 89, 19)
$List11 = GUICtrlCreateList("", 352, 216, 89, 19)
$List12 = GUICtrlCreateList("", 48, 312, 441, 19)
GUISetState(@SW_SHOW)

Global $Baud = 9600, $Databits = 7, $Parity = "O", $Stopbits = 1
Global $ReadTimeout = 5000
Global $CommPort, $Meter, $Message, $Value
Global $SleepBetweenReads = 2000, $SleepAfterSend = 2000

While 1
    SleepWhileGUIGetMsgAllInOne() ; you had your GUI thing here
    $CommPort = 35
    $Meter = 7
    ReadMeter() ;         this is better
    DisplayResults() ;    than the HotKey()
    SleepWhileGUIGetMsgAllInOne($SleepBetweenReads) ; <--- this is the way :)
    $CommPort = 35
    $Meter = 8
    ReadMeter()
    DisplayResults()
    SleepWhileGUIGetMsgAllInOne($SleepBetweenReads)
WEnd

Func SleepWhileGUIGetMsgAllInOne($SleepBetweenReads = 0)
    Local $hTimer = TimerInit()
    Do
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            GUIDelete()
            Exit
    EndSwitch
    Until TimerDiff($hTimer) > $SleepBetweenReads
    Return TimerDiff($hTimer)
EndFunc

Func DisplayResults()
    Select
        Case $Meter = 1
            _GUICtrlListBox_DeleteString($List1, 0)
            _GUICtrlListBox_AddString($List1, $Value)
        Case $Meter = 2
            _GUICtrlListBox_DeleteString($List2, 0)
            _GUICtrlListBox_AddString($List2, $Value)
        Case $Meter = 3
            _GUICtrlListBox_DeleteString($List3, 0)
            _GUICtrlListBox_AddString($List3, $Value)
        Case $Meter = 4
            _GUICtrlListBox_DeleteString($List4, 0)
            _GUICtrlListBox_AddString($List4, $Value)
        Case $Meter = 5
            _GUICtrlListBox_DeleteString($List5, 0)
            _GUICtrlListBox_AddString($List5, $Value)
        Case $Meter = 6
            _GUICtrlListBox_DeleteString($List6, 0)
            _GUICtrlListBox_AddString($List6, $Value)
        Case $Meter = 7
            _GUICtrlListBox_DeleteString($List7, 0)
            _GUICtrlListBox_AddString($List7, $Value)
        Case $Meter = 8
            _GUICtrlListBox_DeleteString($List8, 0)
            _GUICtrlListBox_AddString($List8, $Value)
        Case $Meter = 9
            _GUICtrlListBox_DeleteString($List9, 0)
            _GUICtrlListBox_AddString($List9, $Value)
        Case $Meter = 10
            _GUICtrlListBox_DeleteString($List10, 0)
            _GUICtrlListBox_AddString($List10, $Value)
        Case $Meter = 11
            _GUICtrlListBox_DeleteString($List11, 0)
            _GUICtrlListBox_AddString($List11, $Value)
        Case Else ; If nothing matches then execute the following.
            DisplayMessage("Invalid Case Number")
    EndSelect
    DisplayMessage($Message)
EndFunc

Func DisplayMessage($StringToDisplay)
    _GUICtrlListBox_DeleteString($List12, 0)
    _GUICtrlListBox_AddString($List12, $StringToDisplay)
EndFunc

Func ReadMeter()
    Local $PortHandle, $ReceiveResult, $SendString
    Local $BytesToSend, $BytesSent
    $Message = ""
    DisplayMessage("Reading Meter " & $Meter)
    $PortHandle = _CommAPI_OpenCOMPort($Commport, $Baud, $Parity, $Databits, $StopBits)
    If $PortHandle = 0 Then
        $Value = "Error"
        $Message = "@error return code opening Meter " & $Meter & " was " & @error
        Return
        EndIf
    $SendString = "N" & $Meter & "TA$"
    $BytesToSend = StringLen($SendString)
    $BytesSent = _CommAPI_TransmitString($PortHandle, $SendString, 0)
    SleepWhileGUIGetMsgAllInOne($SleepAfterSend)
    If ($BytesSent <> $BytesToSend) Then
        $Value = "Error"
        $Message = "@error return code sending " & $SendString & " was " & @error
        _CommAPI_ClosePort($PortHandle)
        Return
        EndIf
    $ReceiveResult = _CommAPI_ReceiveString($PortHandle, $ReadTimeout, 0, "", 0)
    _CommAPI_ClosePort($PortHandle)
    If $ReceiveResult = "" Then
        $Value = "Error"
        $Message = "@error return code receiving meter " & $Meter & " was " & @error
        Return
        EndIf
    $Value = StringRight($ReceiveResult, 10)
    $Value = StringStripWS($Value, $STR_STRIPALL) ; forum messing with me !!!
EndFunc

:) 

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...