private void runProgram() { foreach (var process in Process.GetProcessesByName("PCDLRN")) { process.Kill(); } Thread.Sleep(1000); //COPYING .PRG AND .CAD FILE FROM NETWORK FOLDER TO A LOCAL FOLDER (c:\\TEMPCMM\\) themodel.Instance.statusBar = string.Format("PROGRAM DOWNLOADING ..."); FileInfo tempPrg = new FileInfo(themodel.Instance.programPath); FileInfo tempCad = new FileInfo(themodel.Instance.cadPath); FileInfo reportPath = new FileInfo(themodel.Instance.reportPath); DirectoryInfo tempFolder = new DirectoryInfo("c:\\TEMPCMM\\"); if (!Directory.Exists(tempFolder.FullName)) { Directory.CreateDirectory(tempFolder.FullName); } File.Copy(tempPrg.FullName, Path.Combine(tempFolder.FullName, tempPrg.Name), true); File.Copy(tempCad.FullName, Path.Combine(tempFolder.FullName, tempCad.Name), true); //THE PROGRAM FILE tempPrg = new FileInfo(Path.Combine(tempFolder.FullName, tempPrg.Name)); tempCad = new FileInfo(Path.Combine(tempFolder.FullName, tempCad.Name)); PCDLRN.Application PCDApp = null; PCDLRN.IPartPrograms PCDPartPrograms = null; PCDLRN.PartProgram pcdActivePart = null; ApplicationObjectEvents AppEvents = null; try { initPCDMIS(tempPrg, out PCDApp, out PCDPartPrograms, out pcdActivePart); } catch (System.Exception) { MessageBox.Show("PC-DMIS needs to be started!"); // PC-DMIS needs to be started! Thread.CurrentThread.Abort(); } if (PCDApp != null && PCDPartPrograms != null && pcdActivePart != null) { var pcdCommands = pcdActivePart.Commands; int commandCount = pcdCommands.Count; PCDApp.OperatorMode = false; PCDApp.Visible = visible; themodel.Instance.statusBar = string.Format("PROGRAM EXECUTION ..."); try { Thread.Sleep(1000); active_probe msgbox = new active_probe("IS THE PROBE " + PCDApp.DefaultProbeFile + " CURRENTLY ACTIVE ?"); msgbox.ShowDialog(); if (msgbox.yesno) { Thread.Sleep(2000); //execut pcdActivePart.EXECUTE(); pcdActivePart.SaveAs(Path.ChangeExtension(reportPath.FullName, ".PRG")); } } catch (Exception ex) { themodel.Instance.statusBar = string.Format("ERROR DURING EXECUTION"); //try to close the program while (PCDApp.ActivePartProgram != null) { //try to close the program try { pcdActivePart.Quit(); } //if it fails to close, wait 0.5 seconds (then the loop runs again) catch { System.Threading.Thread.Sleep(500); } } File.Delete(tempPrg.FullName); File.Delete(tempCad.FullName); PCDPartPrograms = null; pcdActivePart = null; foreach (var process in Process.GetProcessesByName("PCDLRN")) { process.Kill(); } Thread.CurrentThread.Abort(); } Thread.Sleep(10000); //Quit(); while (PCDApp.ActivePartProgram != null) { //try to close the program try { pcdActivePart.Quit(); } //if it fails to close, wait 0.5 seconds (then the loop runs again) catch { System.Threading.Thread.Sleep(500); } } PCDPartPrograms = null; pcdActivePart = null; } }