I’ve wrote a small program in C# that will be integrated into cruise control (EDIT: oops, pressed enter too early) that creates certain (and not-predefined) JVM’s in their own separate threads. However, when killing the thread the JVM still exists and is not unloaded. This functionality works correctly with .bat files- but if they call a JVM it still remains open still!
Each thread is created from an instance of this class and calls Run()
_Critical is used by the main process for testing reasons.
class BatThread
{
private string _args, _fileName;
private bool _critical;
public ManualResetEvent Flag;
public BatThread(string fileName, string args, bool critical)
{
_fileName = fileName;
_args = args;
_critical = critical;
Flag = new ManualResetEvent(false);
}
public void Run()
{
using (Process Proc = new Process())
{
Proc.StartInfo.FileName = _fileName;
Proc.StartInfo.Arguments = _args;
Proc.StartInfo.RedirectStandardError = false;
Proc.StartInfo.RedirectStandardInput = false;
Proc.StartInfo.RedirectStandardOutput = false;
Proc.StartInfo.UseShellExecute = true;
Proc.Start();
while (true)
{
if (Proc.WaitForExit(100))
{
break;
}
else if (this.Flag.WaitOne(100))
{
Proc.Kill();
break;
}
Thread.Sleep(5000);
}
this.Flag.Set();
}
}
public bool critical { get { return _critical; } }
}
It does not work, because killing the thread does not automatically kills the process (independent) that you spawned.
In general, killing a thread is bad practice. You should signal the thread to exit, so it can do cleanup (tear the process down), and return cleanly.