简体   繁体   中英

Ax2012 form to be opened only once

HI all I am trying to make sure the form is opened only once.For my case where i need to close the old form and open the new form. i tried using the following code but it opens the both forms, please help me in correcting the logic.

    #define.CACHE_OWNER('CsPsqProdTableAttribConfig')
    #define.CACHE_KEY_INSTANCE('Instance')

    FormRun existingForm()
    {
        if (infolog.globalCache().isSet(#CACHE_OWNER, #CACHE_KEY_INSTANCE))
        {
            return infolog.globalCache().get(
                #CACHE_OWNER, #CACHE_KEY_INSTANCE);
        }
        return null;
    }

    void registerThisForm()
    {
        infolog.globalCache().set(#CACHE_OWNER, #CACHE_KEY_INSTANCE, this);
    }

    boolean isAlreadyOpened()
    {
        return existingForm() ? !existingForm().closed() : false;
    }

    void closeExistingForm()
    {
        existingForm().close();
    }

if (isAlreadyOpened())
    {
        closeExistingForm();
        this.activate(true);
    }
    else
    {
        registerThisForm();
    }

I'm guessing that your problem is client/server cache with your singleton pattern.

Look here http://www.axaptapedia.com/Singleton_pattern to see how you can reference the caches.

   SingletonTest   singleton;
   SysGlobalCache  globalCache = infolog.objectOnServer() ? appl.globalCache() : infolog.globalCache();
   ;

   if (globalCache.isSet(classStr(SingletonTest), 0))
       singleton = globalCache.get(classStr(SingletonTest), 0);
   else
   {
       singleton = new SingletonTest();
       infoLog.globalCache().set(classStr(SingletonTest), 0, singleton);
       appl.globalCache().set(classStr(SingletonTest), 0, singleton);
   }

   return singleton;

There must be an easier way of achieving desired behaviour. Eg you can modify the init of the form as follows. No other changes are required.

public void init()
{
    #define.CACHE_OWNER('CsPsqProdTableAttribConfig')
    int hWnd;

    super();

    if (infolog.globalCache().isSet(#CACHE_OWNER, curUserId()))
    {
        hWnd = infolog.globalCache().get(#CACHE_OWNER, curUserId());
    }

    if (WinApi::isWindow(hWnd))
    {
        element.closeCancel();
        WinAPI::bringWindowToTop(hWnd);
    }
    else
    {
        infolog.globalCache().set(#CACHE_OWNER, curUserId(), element.hWnd());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM