I am currently working on a c# project and am using the System.Diagnostic.Process class.
When my program starts it creates a new thread and within each thread starts a different process.
At some point I need to check a setting within my program to see whether each process should continue running or whether it should be stopped but I have no idea how I can reference the process that was started by a certain thread. Each thread I have given a name when the process started but my understanding is c# creates the thread, starts the process and then closes the thread even if the process is still running and is still receiving output.
Is there a way I can find out which process was started by which thread and cancel that processs from running.
But I can't see how I can use this method and the dictionary to be able to stop the process based on this thread name.
UPDATE As request below is the code that I am using that creates each process in the thread. I am using the thread in a dictionary so that I can references it from the outputreceived event but not sure how to do it if I need to close the process.
Thread worker = new Thread(new ThreadStart(() => startProducts(product.Value[0].startScript, product.Value[0].productName)));
worker.IsBackground = false;
worker.Name = product.Value[0].productName;
worker.Start();
logging.logger(string.Format("Starting product '{0}'", product.Value[0].productName));
The method that the thread calls is as follows, this is where each process is started. Each process is guaranteed to have a different named thread, there will never be two threads with the same name.
private void startProducts(string startScript, string product)
{
Process startProductProcess = new Process();
startProductProcess.StartInfo.FileName = startScript;
if ( configManagement.productConfig[product][0].requireArguments == true )
{
startProductProcess.StartInfo.Arguments = configManagement.productConfig[product][0].arguments;
}
startProductProcess.StartInfo.UseShellExecute = false;
startProductProcess.StartInfo.RedirectStandardOutput = true;
StringBuilder processOutput = new StringBuilder("");
startProductProcess.OutputDataReceived += new DataReceivedEventHandler(startProductProcess_OutputDataReceived);
startProductProcess.Exited += new EventHandler(startProductProcess_Exited);
processTag.Add(startProductProcess, product);
startProductProcess.Start();
//Process localByName = Process.GetProcessById(startProductProcess.Id);
startProductProcess.BeginOutputReadLine();
logging.logger(string.Format("Started {0} with: {1} {2}", product,
startProductProcess.StartInfo.FileName, startProductProcess.StartInfo.Arguments));
}