• 可执行文件启动器(上)


    可执行文件启动器可以以一定的时间间隔反复执行外部某个可执行文件。
    如果我们其用源代码生成的jar文件为:Launcher.jar。
    则可以用如下的DOS命令运行它:
    java -jar libLauncher.jar getProcessState.bat 5000
    5000表示每隔5000毫秒执行一次getProcessState.bat 文件。
    源码包含2个文件Worker.java和Launcher.java

    Worker.java文件:

    package com.teleca.robin;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
    import javax.swing.JLabel;
    import javax.swing.JTextField;
     
    public class Worker extends Thread {
        private boolean loop=true;
        private boolean paused=false;
        private int cnt=0;
        final private JLabel consoleText;
        Worker(JLabel lable)
        {
         consoleText=lable;
        }
        private long interval=1000;
        void setInterval(long interval)
        {
         this.interval=interval;
        }
        private String executableFileName;
        void setExecutableFileName(String file)
        {
         if(executableFileName!=null&&file!=null)
         {
         if(!executableFileName.equals(file))
         cnt=0;
         }
         executableFileName=file;
        }
       public void doPause()
        {
         paused=true;
        }
        public void doResume()
        {
         paused=false;
         interrupt();
        }
        public void die()
        {
         loop=false;
        }
        public void run()
        {
         while(loop)
         {
         if(paused||executableFileName==null)
         {
         try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    //e.printStackTrace();
    }
         }
         else
         {
          BufferedReader stdout = null;
          try {
        Process p = null;
        String line = null;
        p = Runtime.getRuntime().exec(executableFileName, null, null);
        stdout = new BufferedReader(new InputStreamReader(p
          .getInputStream()));
        while ((line = stdout.readLine()) != null) {
         System.out.println(line);
        }
        stdout.close();
        stdout=null;
       } catch (IOException e) {
        e.printStackTrace();
       }finally{
       if(stdout!=null)
    try {
    stdout.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
       }
         cnt++;
         if(consoleText!=null)
         consoleText.setText("Execute the file "+cnt+" times");
         try {
    Thread.sleep(interval);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    //e.printStackTrace();
    }
         }
         }
         System.out.println("exit!");
        }
    }

     

  • 相关阅读:
    使用日历控件的一些体会(downmoon)
    带附加条件的NewID()用法
    微软的招聘哲学——做微软人的五大核心素质(摘自《微软360度》)
    彻底禁用fckEditor的上传功能(含防止Type漏洞问题)
    Remote Access Connection Manager 服务因下列错误而停止解决办法
    GridView如何更新批量数据和单条记录?
    .net1.1与.net2.0在加载ascx文件的控件时有所不同(Downmoon)
    牛羊吃草问题求解(downmoon)
    c#操作ecxel的一些资源(downmoon搜集)
    安装sql2008 enterprise (English正式版)图解
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4580670.html
Copyright © 2020-2023  润新知