using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace IOB_MAN { public partial class IOBManPanel : Form { /// /// Elenco dei processi /// protected Dictionary childFormList; public IOBManPanel() { InitializeComponent(); myInit(); } private void myInit() { childFormList = new Dictionary(); MainTimer.Start(); } /// /// apro eseguibile dump /// /// /// private void btnOpen_Click(object sender, EventArgs e) { apriChild(); } private void apriChild() { string path = Application.StartupPath; // da testare x aprire chiudere risorsa... Process childProc = new Process(); ProcessStartInfo psi = new ProcessStartInfo { FileName = string.Format(@"{0}\Test.bat", path), Arguments = "127.0.0.1" }; childProc.StartInfo = psi; Process p = Process.Start(psi); // accodo nuovo IOB... iobAdapt newIob = new iobAdapt(); DateTime adesso = DateTime.Now; newIob.CodIOB = p.Id.ToString(); newIob.startTime = adesso; newIob.pID = p.Id; newIob.isRunning = true; childFormList.Add(newIob.CodIOB, newIob); // aggiorno elenco... childList.Items.Add(p.Id); } /// /// Chiudo primo processo child (se ce ne sono) /// /// /// private void btnClose_Click(object sender, EventArgs e) { chiudiChild(); } private void chiudiChild() { // recupero elenco processi (se almeno 1) if (childFormList.Count > 0) { KeyValuePair kvp = childFormList.First(); int pid = kvp.Value.pID; // provo a vedere SE ci sia il processo e di conseguenza lo chiudo... try { Process p = Process.GetProcessById(pid); // cerco e chiudo quelli che mi interessano... p.CloseMainWindow(); } catch { // errore era già chiuso.. } childList.Items.Remove(pid); childFormList.Remove(kvp.Key); } #if false // Get the current process. Process currentProcess = Process.GetCurrentProcess(); // Get all processes running on the local computer. Process[] localAll = Process.GetProcesses(); // Get all instances of Notepad running on the local computer. // This will return an empty array if notepad isn't running. Process[] localByName = Process.GetProcessesByName("notepad"); #endif } /// /// Effettua tutte le verifiche periodiche... /// /// /// private void MainTimer_Tick(object sender, EventArgs e) { checkRunningchild(); showUpdate(); } /// /// Verifica se i proc child siano ancora in RUN /// private void checkRunningchild() { int pid = 0; List proc2rem = new List(); bool needRem = false; foreach (var kvp in childFormList) { // verifico se esista il processo... int.TryParse(kvp.Key, out pid); try { Process p = Process.GetProcessById(pid); needRem = p.HasExited; } catch { needRem = true; } if (needRem) { proc2rem.Add(pid); } } // ora procdedo alla cancellazione... foreach (var item in proc2rem) { childList.Items.Remove(item); childFormList.Remove(item.ToString()); } } private void showUpdate() { // aggiorna visualizzazioni... } private void IOBManPanel_FormClosing(object sender, FormClosingEventArgs e) { closeAllChild(); } private void closeAllChild() { int pid = 0; // ultimo controllo e chiusura... foreach (var kvp in childFormList) { // verifico se esista il processo... int.TryParse(kvp.Key, out pid); try { Process p = Process.GetProcessById(pid); p.CloseMainWindow(); //childList.Items.Remove(pid); //childFormList.Remove(pid.ToString()); } catch { } } } private void btnStartChild_Click(object sender, EventArgs e) { CForm child = new CForm(); child.MdiParent = this; child.Show(); this.LayoutMdi(MdiLayout.TileHorizontal); } } public class iobAdapt { /// /// Codice univoco macchina /// public string CodIOB { get; set; } /// /// Identificativo univoco processo /// public int pID { get; set; } /// /// DataOra avvio dell'IOB /// public DateTime startTime { get; set; } /// /// Verifica se il processo sia in RUN /// public bool isRunning { get; set; } } }