using System; using System.Collections.Generic; using System.Diagnostics; using System.Windows.Forms; namespace IOB_MAN { public partial class IOBManPanel : Form { /// /// Binding source degli elementi gestiti.. /// private BindingSource ElencoIOB = new BindingSource(); public IOBManPanel() { InitializeComponent(); myInit(); } private void myInit() { // gestione eventi binding source ElencoIOB.AddingNew += ElencoIOB_AddingNew; ElencoIOB.ListChanged += ElencoIOB_ListChanged; // colelgo tab a binding dgvManagedItems.DataSource = ElencoIOB; MainTimer.Start(); } private void ElencoIOB_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e) { updateStatus(); } private void ElencoIOB_AddingNew(object sender, System.ComponentModel.AddingNewEventArgs e) { updateStatus(); } private void updateStatus() { // aggiorno labels lblNumChild.Text = $"Avviati {childList.Items.Count} / {ElencoIOB.Count} processi child"; //dgvManagedItems.bind } /// /// apro eseguibile dump /// /// /// private void btnOpen_Click(object sender, EventArgs e) { apriChild(); updateStatus(); } 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; // aggiorno elenco... childList.Items.Add(p.Id); // aggiungo a datasource ElencoIOB.Add(newIob); } /// /// Chiudo primo processo child (se ce ne sono) /// /// /// private void btnClose_Click(object sender, EventArgs e) { chiudiChild(); updateStatus(); } private void chiudiChild() { int pid = -1; string currSelection = ""; // SOLO SE selezionato in dgv... if (dgvManagedItems.SelectedRows.Count > 0) { foreach (DataGridViewRow riga in dgvManagedItems.SelectedRows) { // chiudo! int.TryParse(riga.Cells["pID"].Value.ToString(), out pid); if (pid >= 0) { // 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.. } // rimuovo da datasource ElencoIOB.RemoveAt(riga.Index); // rimuovo altri childList.Items.Remove(pid); } } } #if false // recupero elenco processi (se almeno 1) if (childFormList.Count > 0) { KeyValuePair kvp = childFormList.First(); // se ce ne fosse uno selezionato uso quello... try { currSelection = childList.SelectedItem.ToString(); if (currSelection != null) { pid = childFormList[currSelection].pID; } else { pid = kvp.Value.pID; } } catch { pid = kvp.Value.pID; } } else { try { currSelection = childList.SelectedItem.ToString(); if (currSelection != null) { pid = childFormList[currSelection].pID; } } catch { } } if (pid >= 0) { // 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.. } // rimuovo da datasource ElencoIOB.Remove(childFormList[currSelection]); // rimuovo altri childList.Items.Remove(pid); childFormList.Remove(currSelection); } #endif #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(); List item2rem = new List(); bool needRem = false; foreach (iobAdapt item in ElencoIOB.List) { // verifico se esista il processo... try { Process p = Process.GetProcessById(item.pID); needRem = p.HasExited; } catch { needRem = true; } if (needRem) { proc2rem.Add(item.pID); item2rem.Add(item); //item.isRunning = false; } } foreach (var item in item2rem) { ElencoIOB.Remove(item); } // ora procdedo alla cancellazione... foreach (var item in proc2rem) { childList.Items.Remove(item); } } private void showUpdate() { // aggiorna visualizzazioni... } private void IOBManPanel_FormClosing(object sender, FormClosingEventArgs e) { closeAllChild(); } private void closeAllChild() { List item2rem = new List(); foreach (iobAdapt item in ElencoIOB.List) { item2rem.Add(item); } foreach (var item in item2rem) { Process p = Process.GetProcessById(item.pID); p.CloseMainWindow(); ElencoIOB.Remove(item); } } private void dgvManagedItems_SelectionChanged(object sender, EventArgs e) { checkButtons(); } /// /// verifica buttons attivi data selezione su gridview... /// private void checkButtons() { bool selected = (dgvManagedItems.SelectedRows.Count > 0); btnClose.Enabled = selected; } private void IOBManPanel_Load(object sender, EventArgs e) { } } }