Page 1 of 1

Re Entrant Programs- How to

Posted: Fri Nov 04, 2016 5:14 am
by bluebird
Friends of HMG

I want to know how to make my program re-entrant.

By that term I mean that if the program functions are completed but you want a message to ask "Press R re re-enter or E to Exit" How do you avoid runtime crashes when a window or control is found to be already declared due to the first pass.

I tried using a flag like STATIC lIsFirstRun:=.T. and setting it .F. at the end when the question is asked and "R" is selected. The intention would be to skip around a DECLARE of a window or control after the first pass.

That does not work if skipping the DECLARE bypasses an ACTION associated with the control.

Is there a better way such as using window release/ restore? I found no Samples of this.
Does a window release include all controls on that window?

Thanks

Re: Re Entrant Programs- How to

Posted: Fri Nov 04, 2016 5:45 am
by Rathinagiri
You can check IsWindowDefined() function to check whether the window is already defined or not.

When you release a window, all the controls will also be released.

Re: Re Entrant Programs- How to

Posted: Fri Nov 04, 2016 8:25 am
by serge_girard
Bluebird,

I use this:

Code: Select all

If !IsWIndowActive (Form_1) 
	DEFINE WINDOW Form_1
	...
	END WINDOW

   CENTER WINDOW   Form_1
   ACTIVATE WINDOW Form_1
ELSE
   Form_OZ_1.SetFocus  
ENDIF
	
Serge

Re: Re Entrant Programs- How to

Posted: Fri Nov 04, 2016 9:05 am
by gfilatov
serge_girard wrote:Bluebird,
I use this:

Code: Select all

If !IsWIndowActive (Form_1) 
	DEFINE WINDOW Form_1
	...
	END WINDOW

   CENTER WINDOW   Form_1
   ACTIVATE WINDOW Form_1
ELSE
   Form_OZ_1.SetFocus  
ENDIF
Hello Bluebird,

Please try the following template application: :arrow:

Code: Select all

#include "hmg.ch"

/* #define COLOR_LightGoldenrod   { 238 , 221 , 130 } */

/******
*
*       Template for application
*
*/

Procedure Main

  SET FONT TO 'Tahoma', 10

  DEFINE WINDOW MainWin ;
         AT 0, 0 ;
         WIDTH 600 ;
         HEIGHT 400 ;
         MAIN ;
         TITLE 'Template Demo' ;
         BACKCOLOR COLOR_LightGoldenrod ;
         ICON "MAIN"

         CreateMainMenu()

         DEFINE STATUSBAR FONT 'Tahoma' SIZE 9
            STATUSITEM 'HMG Power Ready!' 
         END STATUSBAR

         ON KEY ALT+X ACTION { || QuickExit() }

  END WINDOW

  CENTER WINDOW MainWin
  ACTIVATE WINDOW MainWin

Return

****** End of Main ******


/******
*
*       CreateMainMenu()
*
*       Cteate Main Menu
*
*/

Static Procedure CreateMainMenu

  DEFINE MAIN MENU OF MainWin
          
     POPUP '&File'
        ITEM '&Child Window' ACTION Child_Click()
        ITEM '&Modal Window' ACTION Modal_Click()
        SEPARATOR
        ITEM 'E&xit' + Chr(9) + 'Alt+X' ACTION { || QuickExit() }
     END POPUP
                  
  END MENU

Return

****** End of CreateMainMenu ******


/******
*
*       QuickExit()
*
*       Exit from application
*
*/

Static Procedure QuickExit

  QUIT

Return

***** End of QuickExit ******


/******
*
*       Child_Click()
*
*       Cteate Child Window
*
*/

Procedure Child_Click

IF IsWindowDefined ( ChildWin )
   DoMethod ( "ChildWin", "SetFocus" )
   Return
ENDIF

DEFINE WINDOW ChildWin ;
   AT 0, 0 ;
   WIDTH 400 ;
   HEIGHT 300 ;
   CHILD ;
   TITLE 'Child Window'

   ON KEY ESCAPE ACTION ThisWindow.Release()

END WINDOW

ChildWin.Center()

ChildWin.Activate()

Return

***** End of Child_Click ******


/******
*
*       Modal_Click()
*
*       Cteate Modal Window
*
*/

Procedure Modal_Click

DEFINE WINDOW ModalWin ;
   AT 0, 0 ;
   WIDTH 400 ;
   HEIGHT 300 ;
   MODAL ;
   TITLE 'Modal Window'

   ON KEY ESCAPE ACTION ThisWindow.Release()

END WINDOW

ModalWin.Center()

ModalWin.Activate()

Return

***** End of Modal_Click ******
Hope that helps. :idea:

Re: Re Entrant Programs- How to

Posted: Fri Nov 04, 2016 12:32 pm
by Roberto Lopez
bluebird wrote:Friends of HMG
I want to know how to make my program re-entrant.
<...>
Or simply, using NOAUTORELEASE style, then, all the windows are created once at program startup and you show or hide when required, so re-entrance is not a problem at all (you only must skip the code segment where all windows were defined, if one of them (ie: main) was already defined).

There is four NOAUTORELEASE samples in \SAMPLES\BASICS\.

In fact, when I've added this feature, I've hoped that it was massively adopted, but that not happened.

I guess that the Clipper model of defining controls every time were required won, simply because all we are familiar with it, even is (by far) less efficient.

Re: Re Entrant Programs- How to

Posted: Fri Nov 04, 2016 12:46 pm
by Roberto Lopez
And...

I like specially the demo '4'. It is super clean, nice and simple.

This paradigm is wonderful for code maintenance.

It's not required that the windows be created from IDE. Remember that LOAD WINDOW works with code created manually too, so, could be easy to translate your current 'non-IDE' generated forms, to use this scheme.

So... for your problem:

Code: Select all

/*
 * HMG - Harbour Win32 GUI library Demo
 *
 * Copyright 2002 Roberto Lopez <mail.box.hmg@gmail.com>
 * http://www.hmgforum.com//
*/

* NoAutoRelease Style Demo / ACTIVATE WINDOW ALL command

* Using ACTIVATE WINDOW ALL command, all defined windows will be activated 
* simultaneously. NOAUTORELEASE and NOSHOW styles in non-main windows 
* are forced.

#include "hmg.ch"

DECLARE WINDOW Std_Form
DECLARE WINDOW Child_Form
DECLARE WINDOW Topmost_Form
DECLARE WINDOW Modal_Form

Function Main

	If .Not. IsWindowDefined( Main_Form )

	    LOAD WINDOW Main_Form
	    LOAD WINDOW Std_Form
	    LOAD WINDOW Child_Form
	    LOAD WINDOW Topmost_Form
	    LOAD WINDOW Modal_Form

	    ACTIVATE WINDOW ALL 

      EndIf

Return Nil

I like this scheme so much, that this is the way that JMG works (all pages defined at startup, showing each one when required).

Re: Re Entrant Programs- How to

Posted: Sat Nov 05, 2016 11:32 pm
by bluebird
I tried the IsWindowActive() but please look at the code below
When the reentry occurs I get to the diagnostic msgstop() to show that I reached the end but after that the program exits.
I would have thought from the responses that I got that the "Form_2.setfocus would allow me to make a choice via button_1 or _2 set.
However the program trucks on and self-exits.

What am I doing wrong?
Thanks HMG guys
*-----------------------------------------------
FUNCTION MATRIXFUNSTART() //Called from Startup Button
*-----------------------------------------------
STATIC lIsFirstEntry:=.T.
LOCAL lUseSubs:=.t., cYesNo, cDimensions:="?",cFirst,cIsActive
cFirst:=iif(lIsFirstEntry,"First", "ReEntry")


Do while !(cDimensions $"23")
cDimensions:= alltrim(Inputbox("Enter a No of rows and columns","Form_1","?"))
if !(cDimensions $"23")
msgstop(" Can't handle that entry yet")
else
nRows:= val(cDimensions) ; nCols:= nRows
endif
Enddo

DrawMatrixBoxes()

cIsActive:=iif(IsWindowActive (Form_2),"Yep","Nope") // diagnostic
MsgStop("Is Form_2 Active ? "+cIsActive)
//lFEParameter:=lIsFirstEntry
//If lIsFirstEntry
If !IsWIndowActive (Form_2)

DEFINE WINDOW Form_2 ;
AT 290,290 ;
WIDTH 400 ;
HEIGHT 300 ;
BACKCOLOR GREEN ;
TITLE "Choose Data Source"

@ 50 ,100 BUTTON Button_1 ;
PARENT Form_2;
CAPTION "Click to Use Test Values" ;
ONCLICK {||(lUseTestMatrix:=.t.,lIsFirstEntry:=.F., BackToForm_1b())} ;
WIDTH 200 ;
FONT "Arial" SIZE 12 ;
HEIGHT 35

@ 100 ,100 BUTTON Button_2 ;
PARENT Form_2;
CAPTION "Click to enter New Values" ;
ONCLICK {||(lUseTestMatrix:=.f.,lIsFirstEntry:=.F., BackToForm_1b())} ;
FONT "Arial" SIZE 12 ;
WIDTH 200 ;
HEIGHT 35
END WINDOW

ACTIVATE WINDOW Form_2

Else
Form_2.setfocus


msgstop("At end of MatrixFunStart")
Endif //lIsFIrstEntry
RETURN

Posted: Sun Nov 06, 2016 8:07 am
by mol
You should declare NoAutoRelease in form definiton

Re: Re Entrant Programs- How to

Posted: Mon Nov 07, 2016 8:00 am
by bpd2000
Roberto Lopez wrote: I like this scheme so much, that this is the way that JMG works (all pages defined at startup, showing each one when required).
Is the any memory problem when all pages defined at startup

Re: Re Entrant Programs- How to

Posted: Mon Nov 07, 2016 11:26 am
by Roberto Lopez
bpd2000 wrote:
Roberto Lopez wrote: I like this scheme so much, that this is the way that JMG works (all pages defined at startup, showing each one when required).
Is the any memory problem when all pages defined at startup
I haven't any problem.

The memory consumption of Windows definition is minimal.