Files
Mapo-IOB-WIN/IOB-MAN/IOBManPanel.cs
T
2019-12-21 10:46:44 +01:00

322 lines
7.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
namespace IOB_MAN
{
public partial class IOBManPanel : Form
{
/// <summary>
/// Binding source degli elementi gestiti..
/// </summary>
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 {ElencoIOB.Count} processi child";
//dgvManagedItems.bind
}
/// <summary>
/// apro eseguibile dump
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
// aggiungo a datasource
ElencoIOB.Add(newIob);
}
/// <summary>
/// Chiudo primo processo child (se ce ne sono)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClose_Click(object sender, EventArgs e)
{
chiudiChild();
updateStatus();
}
private void chiudiChild()
{
int pid = -1;
// 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);
}
}
}
#if false
// recupero elenco processi (se almeno 1)
if (childFormList.Count > 0)
{
KeyValuePair<string, iobAdapt> 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
}
/// <summary>
/// Effettua tutte le verifiche periodiche...
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainTimer_Tick(object sender, EventArgs e)
{
checkRunningchild();
showUpdate();
}
/// <summary>
/// Verifica se i proc child siano ancora in RUN
/// </summary>
private void checkRunningchild()
{
int pid = 0;
List<int> proc2rem = new List<int>();
List<iobAdapt> item2rem = new List<iobAdapt>();
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;
}
}
// ora procdedo alla cancellazione...
foreach (var item in item2rem)
{
ElencoIOB.Remove(item);
}
}
private void showUpdate()
{
// aggiorna visualizzazioni...
}
private void IOBManPanel_FormClosing(object sender, FormClosingEventArgs e)
{
closeAllChild();
}
private void closeAllChild()
{
List<iobAdapt> item2rem = new List<iobAdapt>();
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();
}
/// <summary>
/// verifica buttons attivi data selezione su gridview...
/// </summary>
private void checkButtons()
{
bool selected = (dgvManagedItems.SelectedRows.Count > 0);
btnClose.Enabled = selected;
}
private void IOBManPanel_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
closeAllChild();
}
private void button2_Click(object sender, EventArgs e)
{
// chiude tutto
closeAllChild();
// FAKE!!! x ora ne apre 10...
for (int i = 0; i < 10; i++)
{
apriChild();
}
}
private void lblNumChild_Click(object sender, EventArgs e)
{
}
private void IOBManPanel_Load_1(object sender, EventArgs e)
{
}
}
}