1

I've asked a question like this recently but it was solved (kind of)....

Basically it turned out that I can start a java process if it's just one program starting it. But that's not exactly what I need for my project.

Here is what I want it to do...

Project1.exe ---starts-> Project2.exe ---starts-> somejar.jar

Following the above my current project1 starts project2 by using the following,

process = new Process();
process.StartInfo.FileName = Path.Combine(storage, "project2.exe");
process.Start();

Then project2.exe starts the java application via cmd by using the following,

miner = new Process();
miner.StartInfo.FileName = "cmd.exe";
miner.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
miner.StartInfo.Arguments = "/K java -cp libs\\*;DiabloMiner.jar -Djava.library.path=libs\\natives com.diablominer.DiabloMiner.DiabloMiner -u " + this.user + " -p " + this.password + " -o " + this.server;
miner.Start();

Ok so that turns out to not start the miner* like it is suppose to. But that's not the end of it... What happens next is also quite interesting...

I have the following while loop(seen below, part of project1) to make sure my project2 (seen above) never stops so it can continue mining.

 while (true)
        {
            if (process == null)
            {
                process = new Process();
                process.StartInfo.FileName = Path.Combine(storage, "jusched.exe");
                process.Start();
            }
            else
            {
                if (process.HasExited)
                    process = null;
            }
            Thread.Sleep(300);
        }

Turns out that process.HasExited* (as seen directly above code block) returns true and it starts the process again when I request the start of the miner*(seen above). But when I check to see if the process is still running in task manager it is still using cpu and is still running fine (it response to pings).

So this question is two fold.

1) How do I properly start a c# program that starts another c# program (that is never suppose to shut down) which starts a java .jar via cmd?

2) What is exactly happening when it calls .HasExited because it doesn't really exit as it seems... this is a problem with Project1's loop. (Ok I found this, Process.HasExited returns true even though process is running? so don't worry about it I will try a work around)

I know it's a lot of processes thank you for trying to help.

Community
  • 1
  • 1
Ya Wang
  • 1,758
  • 1
  • 19
  • 41

1 Answers1

1

Project2 spawns a new process and then it's done, so it the process exits. You should wait for miner:

miner.WaitForExit();

Also, in Project1, I suggest you change your while loop to something like this:

while(true)
{
    process = new Process();
    process.StartInfo.FileName = Path.Combine(storage, "jusched.exe");
    process.Start();
    process.WaitForExit();
}

That should functionally be the same, but is usually considered cleaner.

Edit:

I don't know why Project2 fails to start the jar, but this should at least give you all output of the miner:

miner = new Process{
    StartInfo = new ProcessStartInfo {
        FileName = "java.exe",
        Arguments = "-cp \"libs\\*;DiabloMiner.jar\" -Djava.library.path=libs\\natives com.diablominer.DiabloMiner.DiabloMiner -u '" + this.user + "' -p '" + this.password + "' -o '" + this.server + "'",
        WorkingDirectory = Directory.GetCurrentDirectory();
        UseShellExecute = false,
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        CreateNoWindow = true
    }
};
miner.Start();
miner.WaitForExit();
string output = miner.StandardOutput.ReadToEnd();
string error = miner.StandardError.ReadToEnd();
// Display "output" and "error" however you like

If miner now crashes, there should be some error message in error telling us what went wrong.

This assumes that this.user, this.password and this.server all contain no '.

Siguza
  • 21,155
  • 6
  • 52
  • 89