The Problem : The thread gets abandoned when it starts the process throwing an exception.
EDIT
I'm re-iterating the question along with the thing I'm trying to achieve, How do I start a process from a thread.
Background story
I need the process to run exe's such as imagemagick, libreoffice. I am trying to convert many files and then append their results to a file. There is further processing to be done on the status file later.
I'm not good at threading and I have been referring to some posts on stackoverflow such as this.
I am trying to do something like this :
foreach (Preset pr in listOfPreset)
{
ConvertRipper cRipper = new ConvertRipper(pr);
ThreadStart job = (new ThreadStart(()=> cRipper.Ripper()));
Thread th = new Thread(job);
th.Start();
}
public void Ripper()
{
//create the folders before converting
if (!Directory.Exists(preset.OutputFilePath))
Directory.CreateDirectory(preset.OutputFilePath);
Document document = CreateDocument(preset);
ProcessResult pr = v3Engine.Convert(Const.IMAGEMAGICK, v3Engine.ConvertImages(document));
if (pr.ExitCode == 0)
{
//write status to a file the xml status
}
}
Now somewhere inside the Ripper method I do have a Process which gets started, I basically call a windows exe to convert some files
Convert method
Process proc = new Process();
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Arguments = arguments;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.ErrorDataReceived += (sender, args) => error.Append(args.Data);
proc.OutputDataReceived += (sender, args) => output.Append(args.Data);
proc.Start(); *loc1*
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();
ProcessResult pr = new ProcessResult
{
StandardOutput = output.ToString(),
ErrorOutput = error.ToString(),
ExitCode = proc.ExitCode
};
proc.Close();
return pr;`
EDIT
The stacktrace " at System.Diagnostics.ProcessStartInfo.set_RedirectStandardError(Boolean value)\r\n at Conversion.Converter.AbstractConvertor.Convert(String executor, String arguments) in C:\Users\dev\source\repos\Converstion\Converstion\Converter\AbstractConvertor.cs:line 54"
*The exception state:**
Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
After my process completes. I would like to write the process status to a file.
In the example , I don't understand how it fits into my situation. Because, I have already used this on a method Ripper that indirectly houses the Process.start() inside the Convert method;
P.S: I have read comments such as "Of course, a process starts in a new process (a completely separate bunch of threads), so starting it on a thread is completely pointless."
@Downvoter, could you please share how I could change this question to your liking.
I feel like my scenario needs it. If not, could you suggest what else could I possibly do.
Any tips would be appreciated. Please let me know if you have any questions regarding my tests.