Single instance of a form - Forum - OpenEdge Development - Progress Community
 Forum

Single instance of a form

  • I have a window / form that I must only ever have one instance of. Is there any way of using static or a form property to automatically enable this functionality, or must we do the same as ABL and write code to manage windows ?

  • I have a window / form that I must only ever have one

    instance of. Is there any way of using static or a

    form property to automatically enable this

    functionality, or must we do the same as ABL and

    write code to manage windows ?

    Would form1:ShowDialog() work for you?

    Also, I would think that a form can be launched as a singleton* which might satisfy your requirements.

    However, in either case, there's going to be some ABL code that invokes the window, and at that point you could make the decision whether to show once.

    -- peter

    • but am in the middle of an extremely slow install at the mo, so can't test it myself

  • Search for singleton form.

  • gaakk!. /me's head explodes

  • now to convert this to ABL ...

    {}

  • Easy-peasy

    Frozen-finger typing corrected by:

    Peter Judge

    Message was edited by:

    Peter Judge

  • Easy-peasy

    showoff.

    why a method rather than a property ?

    oh, and shouldn't

    be

    added extra question ...

    Message was edited by:

    Julian Lyndon-Smith

  • First remove the sealed keyword...

    And change the second line to:

    DEFINE PRIVATE STATIC VARIABLE instance AS Singleton NO-UNDO.

    And in an additional STATIC CONSTRUCTOR:

    instance = NEW Singleton();

    As you see, the only constructor in the sample you have given is private (and not static). So the class is the only one who can create an instance and that happens only once - the first time somebody asks for Singleton:Instance.

    Usually you'd replace "Singleton" with the name of your class.

    But I'm not sure, if the Visual Designer will allow a class to be designed with only a private constructor. Somewhere I read, that there needs to be a public constructor in order to satisfy the VD. But that would be an easy test. If you are able to reopen such a Form, it should be o.k..

    I usually maintain a static variable/property in the Form class. In the public non static Constructor, I check if it is set (VALID-OBJECT) and UNDO, THROW ... when so - because there's already an instance. If not, I set that variable to THIS-OBJECT, so that the 2nd instance will detect that it should not be there.

    Mike

  • VD shouldn't have a problem opening the file. You won't be able to extend it though which would be prevented because of the private constructor. In this case you might as well mark the class as FINAL.

    The problem you WILL have with OEA is that you won't be able to run it by just pressing the launch button since OEA doesn't know about the Singleton property or whatever you decide to call it. You will only be able to run it in context of your app.

  • I usually maintain a static variable/property in the

    Form class. In the public non static Constructor, I

    check if it is set (VALID-OBJECT) and UNDO, THROW ...

    when so - because there's already an instance. If

    not, I set that variable to THIS-OBJECT, so that the

    2nd instance will detect that it should not be

    there.

    but how do you return the value of the first instance to the calling program if you've thrown an error ?

  • why a method rather than a property ?

    Good question.

    From http://en.wikipedia.org/wiki/Singleton_pattern, C# uses a property. Java uses a method. I've a tendency to follow the Java style, but in the ABL either will suffice and they'll both perform the same.

    Actually, I like this approach more , now that I think about it. /me will now update my singletons.

    be

    Yes. I will claim that was a Frozen Finger Foulup ...

    -- peter

  • The example is incomplete. What if someone calls dispose? The object handle would still be valid, but trying to call Show() on it would be a disaster

    foo:GetInstance():Dispose().

    You should override the dispose method and set the mFoo variable to unknown(?)

    Message was edited by:

    Matthew Baker

  • won't the instance property return a new foo every time ? Don't you have to add a static var, set that if not a valid object, and return the var ?

  • urk. something is not right - probably in my mind

    run this code:

    you get a "new foo" message everytime you run the program. What am I missing ?

  • How are you running this? New prowin32.exe session each time, or same session?