JavaExe and Java application as windows system service interactive to desktop

◇◆丶佛笑我妖孽 提交于 2019-11-29 15:32:00

you should have in your case :

public class MyApp_ServiceManagement
{
    static boolean isMsgToDisplay = false;

    /////////////////////////////
    public static boolean serviceInit()
    {
        (new Thread()
        {
            public void run()
            {
                for(int i=0;i < 6;i++)
                {
                     try { sleep(5*1000); }
                     catch(Exception ex) {}

                     isMsgToDisplay = true;
                }
            }
        }).start();

        return true;
    }

    /// is Data ready to be send to the UI ?
    public static boolean serviceIsDataForUI()
    {
        return isMsgToDisplay;
    }

    /// Data to be send to the UI
    public static Serializable serviceDataForUI()
    {
        isMsgToDisplay = false;
        return "hello, I am an interactive Service";
    }
}

and for the UI part :

public class MyApp_TaskbarManagement
{
    /// To show (or not) the icon in tray
    public static boolean taskIsShow()
    {
        return false;
    }

    /// Receive the message from Service
    public static void taskDataFromService(Serializable data)
    {
        JOptionPane.showMessageDialog(null, data);
    }

    /// descr of UI
    public static String[] taskGetInfo()
    {
        return new String[]
            {
                "UI part of Service"
            };
    }
}

the main() method is never called in service mode (except one particular case), but if you want keep your main() method you must put a call to main() in serviceInit().

Put serviceInit() in your main class or in another class named XXX_ServiceManagement where XXX is the name of your main class.

Then, serviceInit() must return before a 30 seconds delay. Don't put a infinite loop, ... in it. Put your code in a thread, and start it from serviceInit() (or main)

That answer to your problem?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!