gpt4 book ai didi

java - 如何根据用户输入运行某些进程,然后相应地销毁它们?

转载 作者:行者123 更新时间:2023-12-01 14:21:57 25 4
gpt4 key购买 nike

例如:

Runtime rt = Runtime.getRuntime();创建Runtime rt

Process p1 = rt.exec("C:/Windows/System32/calc.exe");创建Process p1Runtime rt .

然后p1.destroy();将摧毁Process p1 .

我的问题是:如果我有多个Process (例如 p1p2p3 ),我该如何 destroy()一次性全部消灭掉,而不是一一消灭?

对于列表,我如何在我的代码中实现列表?我还没有使用过列表和数组,尽管我尝试过学习如何使用它们,但我想这是通过示例进行教学更容易的事情之一......

另外,一个 for-loopdestroy()已经向我提到了这些流程,但是我该如何使用 Process 来解决这个问题? ?

这是我的代码:

package tf2_account_chief;

import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TF2_Account_Chief extends JPanel implements ActionListener {

private static JFrame f = new JFrame("TF2 Account C.H.I.E.F.");

private JLabel runL = new JLabel("Check box to run!");
private JLabel runnableTogetherLabel = new JLabel("How many idlers would you like to run at a time?");
private JCheckBox boxRun = new JCheckBox();
private static JTextField runnableTogetherInput = new JTextField();
private JButton runButton = new JButton("Run!");

private static Image BGimg;

private static int allSets, setsRun, runnableTogether, totalRunnable, run;

private static int processesArray;


private static final String dir = System.getProperty("user.dir");

public void launchFrame() {

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack(); //Adjusts panel to components for display
f.setVisible(true);

f.add(runL);
f.add(boxRun);
f.add(runnableTogetherInput);
f.add(runnableTogetherLabel);
f.add(runButton);

f.setSize(500, 500);
runL.setSize(500, 50);
boxRun.setSize(20, 15);
runnableTogetherInput.setSize(25, 15);
runnableTogetherLabel.setSize(275, 25);
runButton.setSize(60, 25);

f.setLocation(0, 0);
runL.setLocation(50, 50);
boxRun.setLocation(95, 100);
runnableTogetherInput.setLocation(285, 5);
runnableTogetherLabel.setLocation(0, 0);
runButton.setLocation(325, 0);

boxRun.addActionListener(this);
runButton.addActionListener(this);

}

private void Processes() throws IOException, InterruptedException {
Runtime rt = Runtime.getRuntime();

// "runnableTogether" will be the number that the user inputs in the GUI

switch (runnableTogether) {

case 128:
Process p128 = rt.exec(dir + "/run/Idle128.bat");
case 127:
Process p127 = rt.exec(dir + "/run/Idle127.bat");
case 126:
Process p126 = rt.exec(dir + "/run/Idle126.bat");
case 125:
Process p125 = rt.exec(dir + "/run/Idle125.bat");
case 124:
Process p124 = rt.exec(dir + "/run/Idle124.bat");
case 123:
Process p123 = rt.exec(dir + "/run/Idle123.bat");
case 122:
Process p122 = rt.exec(dir + "/run/Idle122.bat");
case 121:
Process p121 = rt.exec(dir + "/run/Idle121.bat");
case 120:
Process p120 = rt.exec(dir + "/run/Idle120.bat");
case 119:
Process p119 = rt.exec(dir + "/run/Idle119.bat");
case 118:
Process p118 = rt.exec(dir + "/run/Idle118.bat");
case 117:
Process p117 = rt.exec(dir + "/run/Idle117.bat");
case 116:
Process p116 = rt.exec(dir + "/run/Idle116.bat");
case 115:
Process p115 = rt.exec(dir + "/run/Idle115.bat");
case 114:
Process p114 = rt.exec(dir + "/run/Idle114.bat");
case 113:
Process p113 = rt.exec(dir + "/run/Idle113.bat");
case 112:
Process p112 = rt.exec(dir + "/run/Idle112.bat");
case 111:
Process p111 = rt.exec(dir + "/run/Idle111.bat");
case 110:
Process p110 = rt.exec(dir + "/run/Idle110.bat");
case 109:
Process p109 = rt.exec(dir + "/run/Idle109.bat");
case 108:
Process p108 = rt.exec(dir + "/run/Idle108.bat");
case 107:
Process p107 = rt.exec(dir + "/run/Idle107.bat");
case 106:
Process p106 = rt.exec(dir + "/run/Idle106.bat");
case 105:
Process p105 = rt.exec(dir + "/run/Idle105.bat");
case 104:
Process p104 = rt.exec(dir + "/run/Idle104.bat");
case 103:
Process p103 = rt.exec(dir + "/run/Idle103.bat");
case 102:
Process p102 = rt.exec(dir + "/run/Idle102.bat");
case 101:
Process p101 = rt.exec(dir + "/run/Idle101.bat");
case 100:
Process p100 = rt.exec(dir + "/run/Idle100.bat");
case 99:
Process p99 = rt.exec(dir + "/run/Idle99.bat");
case 98:
Process p98 = rt.exec(dir + "/run/Idle98.bat");
case 97:
Process p97 = rt.exec(dir + "/run/Idle97.bat");
case 96:
Process p96 = rt.exec(dir + "/run/Idle96.bat");
case 95:
Process p95 = rt.exec(dir + "/run/Idle95.bat");
case 94:
Process p94 = rt.exec(dir + "/run/Idle94.bat");
case 93:
Process p93 = rt.exec(dir + "/run/Idle93.bat");
case 92:
Process p92 = rt.exec(dir + "/run/Idle92.bat");
case 91:
Process p91 = rt.exec(dir + "/run/Idle91.bat");
case 90:
Process p90 = rt.exec(dir + "/run/Idle90.bat");
case 89:
Process p89 = rt.exec(dir + "/run/Idle89.bat");
case 88:
Process p88 = rt.exec(dir + "/run/Idle88.bat");
case 87:
Process p87 = rt.exec(dir + "/run/Idle87.bat");
case 86:
Process p86 = rt.exec(dir + "/run/Idle86.bat");
case 85:
Process p85 = rt.exec(dir + "/run/Idle85.bat");
case 84:
Process p84 = rt.exec(dir + "/run/Idle84.bat");
case 83:
Process p83 = rt.exec(dir + "/run/Idle83.bat");
case 82:
Process p82 = rt.exec(dir + "/run/Idle82.bat");
case 81:
Process p81 = rt.exec(dir + "/run/Idle81.bat");
case 80:
Process p80 = rt.exec(dir + "/run/Idle80.bat");
case 79:
Process p79 = rt.exec(dir + "/run/Idle79.bat");
case 78:
Process p78 = rt.exec(dir + "/run/Idle78.bat");
case 77:
Process p77 = rt.exec(dir + "/run/Idle77.bat");
case 76:
Process p76 = rt.exec(dir + "/run/Idle76.bat");
case 75:
Process p75 = rt.exec(dir + "/run/Idle75.bat");
case 74:
Process p74 = rt.exec(dir + "/run/Idle74.bat");
case 73:
Process p73 = rt.exec(dir + "/run/Idle73.bat");
case 72:
Process p72 = rt.exec(dir + "/run/Idle72.bat");
case 71:
Process p71 = rt.exec(dir + "/run/Idle71.bat");
case 70:
Process p70 = rt.exec(dir + "/run/Idle70.bat");
case 69:
Process p69 = rt.exec(dir + "/run/Idle69.bat");
case 68:
Process p68 = rt.exec(dir + "/run/Idle68.bat");
case 67:
Process p67 = rt.exec(dir + "/run/Idle67.bat");
case 66:
Process p66 = rt.exec(dir + "/run/Idle66.bat");
case 65:
Process p65 = rt.exec(dir + "/run/Idle65.bat");
case 64:
Process p64 = rt.exec(dir + "/run/Idle64.bat");
case 63:
Process p63 = rt.exec(dir + "/run/Idle63.bat");
case 62:
Process p62 = rt.exec(dir + "/run/Idle62.bat");
case 61:
Process p61 = rt.exec(dir + "/run/Idle61.bat");
case 60:
Process p60 = rt.exec(dir + "/run/Idle60.bat");
case 59:
Process p59 = rt.exec(dir + "/run/Idle59.bat");
case 58:
Process p58 = rt.exec(dir + "/run/Idle58.bat");
case 57:
Process p57 = rt.exec(dir + "/run/Idle57.bat");
case 56:
Process p56 = rt.exec(dir + "/run/Idle56.bat");
case 55:
Process p55 = rt.exec(dir + "/run/Idle55.bat");
case 54:
Process p54 = rt.exec(dir + "/run/Idle54.bat");
case 53:
Process p53 = rt.exec(dir + "/run/Idle53.bat");
case 52:
Process p52 = rt.exec(dir + "/run/Idle52.bat");
case 51:
Process p51 = rt.exec(dir + "/run/Idle51.bat");
case 50:
Process p50 = rt.exec(dir + "/run/Idle50.bat");
case 49:
Process p49 = rt.exec(dir + "/run/Idle49.bat");
case 48:
Process p48 = rt.exec(dir + "/run/Idle48.bat");
case 47:
Process p47 = rt.exec(dir + "/run/Idle47.bat");
case 46:
Process p46 = rt.exec(dir + "/run/Idle46.bat");
case 45:
Process p45 = rt.exec(dir + "/run/Idle45.bat");
case 44:
Process p44 = rt.exec(dir + "/run/Idle44.bat");
case 43:
Process p43 = rt.exec(dir + "/run/Idle43.bat");
case 42:
Process p42 = rt.exec(dir + "/run/Idle42.bat");
case 41:
Process p41 = rt.exec(dir + "/run/Idle41.bat");
case 40:
Process p40 = rt.exec(dir + "/run/Idle40.bat");
case 39:
Process p39 = rt.exec(dir + "/run/Idle39.bat");
case 38:
Process p38 = rt.exec(dir + "/run/Idle38.bat");
case 37:
Process p37 = rt.exec(dir + "/run/Idle37.bat");
case 36:
Process p36 = rt.exec(dir + "/run/Idle36.bat");
case 35:
Process p35 = rt.exec(dir + "/run/Idle35.bat");
case 34:
Process p34 = rt.exec(dir + "/run/Idle34.bat");
case 33:
Process p33 = rt.exec(dir + "/run/Idle33.bat");
case 32:
Process p32 = rt.exec(dir + "/run/Idle32.bat");
case 31:
Process p31 = rt.exec(dir + "/run/Idle31.bat");
case 30:
Process p30 = rt.exec(dir + "/run/Idle30.bat");
case 29:
Process p29 = rt.exec(dir + "/run/Idle29.bat");
case 28:
Process p28 = rt.exec(dir + "/run/Idle28.bat");
case 27:
Process p27 = rt.exec(dir + "/run/Idle27.bat");
case 26:
Process p26 = rt.exec(dir + "/run/Idle26.bat");
case 25:
Process p25 = rt.exec(dir + "/run/Idle25.bat");
case 24:
Process p24 = rt.exec(dir + "/run/Idle24.bat");
case 23:
Process p23 = rt.exec(dir + "/run/Idle23.bat");
case 22:
Process p22 = rt.exec(dir + "/run/Idle22.bat");
case 21:
Process p21 = rt.exec(dir + "/run/Idle21.bat");
case 20:
Process p20 = rt.exec(dir + "/run/Idle20.bat");
case 19:
Process p19 = rt.exec(dir + "/run/Idle19.bat");
case 18:
Process p18 = rt.exec(dir + "/run/Idle18.bat");
case 17:
Process p17 = rt.exec(dir + "/run/Idle17.bat");
case 16:
Process p16 = rt.exec(dir + "/run/Idle16.bat");
case 15:
Process p15 = rt.exec(dir + "/run/Idle15.bat");
case 14:
Process p14 = rt.exec(dir + "/run/Idle14.bat");
case 13:
Process p13 = rt.exec(dir + "/run/Idle13.bat");
case 12:
Process p12 = rt.exec(dir + "/run/Idle12.bat");
case 11:
Process p11 = rt.exec(dir + "/run/Idle11.bat");
case 10:
Process p10 = rt.exec(dir + "/run/Idle10.bat");
case 9:
Process p9 = rt.exec(dir + "/run/Idle9.bat");
case 8:
Process p8 = rt.exec(dir + "/run/Idle8.bat");
case 7:
Process p7 = rt.exec(dir + "/run/Idle7.bat");
case 6:
Process p6 = rt.exec(dir + "/run/Idle6.bat");
case 5:
Process p5 = rt.exec(dir + "/run/Idle5.bat");
case 4:
Process p4 = rt.exec(dir + "/run/Idle4.bat");
case 3:
Process p3 = rt.exec(dir + "/run/Idle3.bat");
case 2:
Process p2 = rt.exec(dir + "/run/Idle2.bat");
case 1:
Process p1 = rt.exec(dir + "/run/Idle1.bat");
Thread.sleep(5000);
//destroy all Processes here or at destroyProcesses()

break;

default:
System.exit(0);

}

}

private void processesAray() {



}

private void destroyProcesses() {



}

public void actionPerformed(final ActionEvent e) {

if (boxRun.isSelected()) {

System.out.println("running!");

try {

if (runnableTogether > 0 && runnableTogether < 128) {

firstEvent1();

}

else {

System.exit(0);

}

}

catch(Exception ex) {

}

}

else {

System.out.println("not running!");

}

if(e.getSource() == runButton){

//runnableTogetherString = (runnableTogetherInput.getText());

runnableTogether = Integer.parseInt(runnableTogetherInput.getText());

try {

if (runnableTogether > 0 && runnableTogether < 128) {

firstEvent1();

}

else {

System.exit(0);

}

}

catch(Exception ex) {

}

}

}

private void firstEvent1() throws IOException {
totalRunnable = runnableTogether*17;
try {
if (allSets <= totalRunnable) {
if (setsRun <= runnableTogether) {
Processes();

setsRun++;
/*do {
}while(setsRun <= runnableTogether);*/
Thread.sleep(5000);
//p1.destroy(); // Does not work right now
/*p2.destroy();
p3.destroy();
p4.destroy();
p5.destroy();
p6.destroy();
p7.destroy();
p8.destroy();*/

}
allSets = allSets + runnableTogether;
}
} catch (Exception ex) {
}
}

public static void main(String[] args) throws IOException {

TF2_Account_Chief gui = new TF2_Account_Chief();
gui.launchFrame();

Container contentPane = f.getContentPane();
contentPane.add(new TF2_Account_Chief());

//BGimg = ImageIO.read(new File(dir + "/tf2.jpg"));

}

public void paintComponent(Graphics g) {

//g.drawImage(BGimg, 0, 0, null);

}

}

有人知道怎么做吗?

最佳答案

您可以创建一个ArrayList或进程

// Create runtime and array
Runtime rt = Runtime.getRuntime();
List<Process> processes = new ArrayList<Process>();

swtich(bleh){
....
// Add every process
case 1:
processes.add(rt.exec("my command 1"));

case 2:
processes.add(rt.exec("my command 2"));
....
case n:
processes.add(rt.exec("my command n"));
}

// Destroy all items in the list, iterating the array
destroyProcesses(processes)

基本实现可能在哪里

private void destroyProcesses(List<Process> processes){

if (processes == null)
return;
else{
for (Process thisProcess : processes)
thisProcess.destroy();

processes.clear();
}
}

关于java - 如何根据用户输入运行某些进程,然后相应地销毁它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17462023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com