Sign in to follow this  
Followers 0
melkortheevil

Return control to GUI and Stop running function

5 posts in this topic

#1 ·  Posted (edited)

Friends,

I am really desperate and I hope you can help me. I coded my first GUI script to send free SMS.

Description: When $Button1 is pressed the main function starts. It takes about 2 minutes to complete because it loads few web pages and insert some data as logon credentials, recipient number and text message itself.

1. When the button is pressed the GUI is "frozen" until main function is completed. Is it possible to return the control to GUI and press the same button or other buttons to e.g. change global variable?

2. Is it possible to stop running function? I found many examples but all are related to very fast calculations. There is no calculation in my function it simply loads the web pages and retrieve/insert some data so I don't know if there is a place for any FOR or While loop.

I tried to achieve this two goals with both "On Event" and "Case Switch" solution so my code is really dirty now. I am going to provide working concept.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <GuiStatusBar.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
#include <Array.au3>

Opt("GUIOnEventMode", 1)

$Form1 = GUICreate("Meteor SMS Agent v0.2", 304, 329, 246, 160)
   GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")
$file = GUICtrlCreateMenu("&File")
$menu_exit = GUICtrlCreateMenuItem("Exit", $file)
   GUICtrlSetOnEvent(-1, "_Quit")
$help = GUICtrlCreateMenu("Help")
$menu_about = GUICtrlCreateMenuItem("About", $help)
   GUICtrlSetOnEvent(-1, "about")
$hListBox = GUICtrlCreateList("", 192, 96, 89, 118, $LBS_STANDARD)
$Button1 = GUICtrlCreateButton("Send SMS", 48, 224, 83, 33, $WS_GROUP)
   GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
   ;GUICtrlSetOnEvent(-1, "_StartStopButton")
   GUICtrlSetOnEvent(-1, "smssend")
$Input1 = GUICtrlCreateInput("085", 16, 32, 89, 24, $ES_NUMBER)
   GUICtrlSetLimit(-1, 10)
$Edit1 = GUICtrlCreateEdit("", 16, 96, 145, 121, BitOR($LBS_STANDARD, $WS_VSCROLL) )
   GUICtrlSetLimit(-1, 160)
   GUICtrlSetData(-1, "")
$Label1 = GUICtrlCreateLabel("Your phone number", 16, 8, 121, 20)
$Label2 = GUICtrlCreateLabel("Message (160 chars)", 16, 72, 129, 20)
$Label3 = GUICtrlCreateLabel("Contact List", 192, 72, 72, 20)
$Input2 = GUICtrlCreateInput("", 192, 32, 57, 24, BitOR($ES_PASSWORD, $ES_NUMBER))
   GUICtrlSetLimit(-1, 7)
$Label4 = GUICtrlCreateLabel("PIN (MyMeteor)", 192, 8, 97, 20)
$Progress1 = GUICtrlCreateProgress(50, 264, 77, 10)
$Input3 = GUICtrlCreateInput("300 / 300", 216, 232, 57, 24,$ES_READONLY)
$Label5 = GUICtrlCreateLabel("Free SMS", 216, 256, 64, 20)
;$help = GUICtrlCreateMenu("Help")
;$menu_about = GUICtrlCreateMenuItem("About", $help)
$StatusBar = _GUICtrlStatusBar_Create($Form1, -1, "Ready")

GUISetState(@SW_SHOW)

While 1
sleep(100)
WEnd

Func smssend()
;THIS IS THE MAIN FUNCTION - it takes about 2 minutes to complete, It loads different web pages
EndFunc

Func _quit()
exit
EndFunc

Func about()
MsgBox(8192 + 262144 + 64, "Information about...", "I am desperate :)")
EndFunc
Edited by melkortheevil

Share this post


Link to post
Share on other sites



The code below shows a method which might work for you. I put something in the smsend function that takes a long time.

First try the script as shown below. You will find the problem you described.

Then change the variable $WITHFIX to be 1 and try again.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <GuiStatusBar.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
#include <Array.au3>

Global $WITHFIX = 0;set to 1 to try with the fix, but run with = 0 first

Opt("GUIOnEventMode", 1)

$Form1 = GUICreate("Meteor SMS Agent v0.2", 304, 329, 246, 160)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")
$file = GUICtrlCreateMenu("&File")
$menu_exit = GUICtrlCreateMenuItem("Exit", $file)
GUICtrlSetOnEvent(-1, "_Quit")
$help = GUICtrlCreateMenu("Help")
$menu_about = GUICtrlCreateMenuItem("About", $help)
GUICtrlSetOnEvent(-1, "about")


$hListBox = GUICtrlCreateList("", 192, 96, 89, 118, $LBS_STANDARD)
$Button1 = GUICtrlCreateButton("Send SMS", 48, 224, 83, 33, $WS_GROUP)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
;GUICtrlSetOnEvent(-1, "_StartStopButton")

If $WITHFIX Then
    GUICtrlSetOnEvent(-1, "smssend")
Else
    GUICtrlSetOnEvent(-1, "Realsmssend")
EndIf

$Input1 = GUICtrlCreateInput("085", 16, 32, 89, 24, $ES_NUMBER)
GUICtrlSetLimit(-1, 10)
$Edit1 = GUICtrlCreateEdit("", 16, 96, 145, 121, BitOR($LBS_STANDARD, $WS_VSCROLL))
GUICtrlSetLimit(-1, 160)
GUICtrlSetData(-1, "")
$Label1 = GUICtrlCreateLabel("Your phone number", 16, 8, 121, 20)
$Label2 = GUICtrlCreateLabel("Message (160 chars)", 16, 72, 129, 20)
$Label3 = GUICtrlCreateLabel("Contact List", 192, 72, 72, 20)
$Input2 = GUICtrlCreateInput("", 192, 32, 57, 24, BitOR($ES_PASSWORD, $ES_NUMBER))
GUICtrlSetLimit(-1, 7)
$Label4 = GUICtrlCreateLabel("PIN (MyMeteor)", 192, 8, 97, 20)
$Progress1 = GUICtrlCreateProgress(50, 264, 77, 10)
$Input3 = GUICtrlCreateInput("300 / 300", 216, 232, 57, 24, $ES_READONLY)
$Label5 = GUICtrlCreateLabel("Free SMS", 216, 256, 64, 20)
;$help = GUICtrlCreateMenu("Help")
;$menu_about = GUICtrlCreateMenuItem("About", $help)
$StatusBar = _GUICtrlStatusBar_Create($Form1, -1, "Ready")

GUISetState(@SW_SHOW)

$startsmssend = False
While 1
    Sleep(100)
    If $startsmssend Then
    Realsmssend()
    $startsmssend = False
    EndIf

WEnd

Func smssend()
    $startsmssend = True
EndFunc ;==>smssend


Func Realsmssend()

    ;THIS IS THE MAIN FUNCTION - it takes about 2 minutes to complete, It loads different web pages
    For $ir = 0 To 1000
    Sleep(50)
    ConsoleWrite($ir & @CRLF)
    Sleep(50)
    Next
EndFunc ;==>Realsmssend

Func _quit()
    Exit
EndFunc ;==>_quit

Func about()
    MsgBox(8192 + 262144 + 64, "Information about...", "I am desperate :)")
EndFunc ;==>about

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks martin, I got your point. You are star. Nice solution.

I slightly modified your code to implement Start/Stop functionaly with one button.

$running = True
$startsmssend = False
While 1
    Sleep(100)
    If $startsmssend Then
    smssend()
    GUICtrlSetData($Button1, "SendSMS")
    $startsmssend = False
    EndIf
WEnd

Func smssendbutton()
    $sButtonText = GUICtrlRead ($Button1)
    If $sButtonText = "Send SMS" Then ; Change the text on Button on click
    GUICtrlSetData($Button1, "STOP")
    Else
    GUICtrlSetData($Button1, "Send SMS")
    EndIf
    $running = Not $running ;$running is False on first click
    If ($running = True) And ($startsmssend = True) Then _Quit()
    $startsmssend = True
EndFunc ;==>smssendbutton

I have one additional question. How to close IE window created in main smssend() function? I am trying to cancel the process with the same button which calls _Quit() function. Bellow are the ideas which don't work for me now.

Func _Quit()
    ;If WinExists($IEWnd) then $oIE.Quit
    ;_IEQuit ($oIE)
    ;_IEAction ( $oIE, "quit")
EndFunc
Edited by melkortheevil

Share this post


Link to post
Share on other sites

...

I have one additional question. How to close IE window created in main smssend() function? I am trying to cancel the process with the same button which calls _Quit() function. Bellow are the ideas which don't work for me now.

Func _Quit()
    ;If WinExists($IEWnd) then $oIE.Quit
    ;_IEQuit ($oIE)
    ;_IEAction ( $oIE, "quit")
    ;If WinExists($IEWnd) then $oIE.Quit
EndFunc

I don't know without seeing more of your code, and might not even if I did see it because I am a noob with _IE*.

_IEQuit($oIE) should work but not if you used _IECreateEmbedded. See the help for _IEQuit.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Thanks martin. I found that I can close IE window if $oIE = _IECreate() is used and _IENavigate after that.

At the moment I have two issues not only with IE so maybe I will create a new thread with a code you taught me in this lesson ;)

Maybe you can still help me. Is it possible smoothly terminate a function? I am able,in some cases,kill IE window which is created at the begining of the main function. The problem is that the function is still running on backgroud.

#include <IE.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>

Global $oIE, $oHWND
Global $Form1, $Button1
Opt("GUIOnEventMode", 1)

$Form1 = GUICreate("Meteor SMS Agent v0.2", 304, 329, 246, 160)
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
$Button1 = GUICtrlCreateButton("Run IE", 48, 224, 83, 33)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "smssendbutton")

 GUISetState()

$running = True
$startsmssend = False
While 1
    Sleep(100)
    If $startsmssend Then
    smssend()
    $startsmssend = False
    EndIf

WEnd

Func smssendbutton()
    If $startsmssend = True Then MsgBox(4160, "Information", "Function is already running!")
    $running = Not $running ;$running is False on first click
    If ($running = True) And ($startsmssend = True) Then _Quit()
    If $startsmssend = False then $startsmssend = True
EndFunc ;==>smssendbutton

Func smssend()  ;THIS IS THE MAIN FUNCTION
$oIE = _IECreate ()
;$oIE = _IECreate ("https://www.mymeteor.ie/go/login") ;it's still loading, can't terminate it
$oHWND = _IEPropertyGet($oIE, "hwnd")
_IENavigate($oIE, "https://www.mymeteor.ie/go/login")
_IENavigate($oIE, "http://www.autoitscript.com")
   For $ir = 0 To 1000
  Sleep(50)
   ConsoleWrite($ir & @CRLF)
   Sleep(50)
   Next

EndFunc

Func _Quit()
_IEQuit ($oIE) ;first try
$oIE=0
;_IEAction ( $oIE, "quit") ; second try
;If WinExists($oHWND) then $oIE.Quit ; third try
ConsoleWrite ( "Done" )
EndFunc

Func Quit()
Exit
EndFunc

Issues

1. If you don't wait long enough to see autoit web site loaded there is an error message in AutoIt cosole.

IE.au3 (586) : ==> Variable must be of type "Object".

2. If you wait long enough you can close IE but the function is still running.

Share this post


Link to post
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
Sign in to follow this  
Followers 0