我在线程中使用Java进程构建器启动了命令行perl进程。当我关闭我的javafx应用程序时,perl.exe进程一直在运行。
我想关闭cmd进程的输入流并停止该线程。
我已尝试过以下步骤:
代码:
MyRunnable CLASS:
public class MyRunnable implements Runnable
{
private final int value;
private Boolean stop = false;
MyRunnable(int value)
{
this.value = value;
}
@Override
public void run()
{
try
{
ProcessBuilder pb2 = new ProcessBuilder(new String[] { "cmd.exe", "/c", "perl E:\\PerlRunner\\SCRIPTS\\AIRPLANE.pl SP9830A510159222101"});
Process pr12=pb2.start();
Thread.sleep(1000);
BufferedReader brq = new BufferedReader(new InputStreamReader(pr12.getInputStream()));
while(brq.readLine()!=null && Variables.flag )
{
System.out.println(brq.readLine()+" FLAG:IN WHILE"+Variables.flag);
}
if(!Variables.flag)
{
pr12.destroy();
System.out.println("PROCESS DESTOYED:"+Variables.flag);
}
}
catch(IOException ee)
{
}
catch(InterruptedException rr)
{
}
}
}
主要课程:
两个按钮:
播放:
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Variables.flag=true;
System.out.println("Start");
MyRunnable n1= new MyRunnable(0);
Thread t1=new Thread(n1);
t1.start();
}});
STOP BUTTON:
btn1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Variables.flag=false;
}});
Variables.flag是在另一个类中定义的静态变量。
Perl进程:
#!/usr/bin/perl
$devices = $ARGV[0];
$temp = 0;
$LOGFILE = "E:\\PerlRunner\\times.txt";
open(LOGFILE) or die("Could not open log file.");
foreach $line (<LOGFILE>) {
if (index($line, "flmTimes") != -1)
{
my $result = index($line, "=");
$temp = substr($line, $result + 1);
}
sleep(1);
}
for($i=1; $i<=$temp;$i++)
{
system("adb -s $devices shell am start -a
android.settings.AIRPLANE_MODE_SETTINGS");
sleep 5;
system("adb -s $devices shell input tap 200 200");
sleep 35;
system("adb -s $devices shell input keyevent KEYCODE_HOME");
sleep 3;
}
times.txt :
testTimes=1
flmTimes=3
mtcTimes=1
caseTimes=1