273 lines
6.7 KiB
C#
273 lines
6.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Windows.Forms;
|
|
|
|
namespace IOB_MAN
|
|
{
|
|
public partial class IOBManPanel : Form
|
|
{
|
|
/// <summary>
|
|
/// Elenco dei processi
|
|
/// </summary>
|
|
protected Dictionary<string, iobAdapt> childFormList;
|
|
/// <summary>
|
|
/// Binding source degli elementi gestiti..
|
|
/// </summary>
|
|
private BindingSource ElencoIOB = new BindingSource();
|
|
|
|
public IOBManPanel()
|
|
{
|
|
InitializeComponent();
|
|
myInit();
|
|
}
|
|
|
|
private void myInit()
|
|
{
|
|
childFormList = new Dictionary<string, iobAdapt>();
|
|
// 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 {childFormList.Count} / {childList.Items.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;
|
|
childFormList.Add(newIob.CodIOB, newIob);
|
|
// aggiorno elenco...
|
|
childList.Items.Add(p.Id);
|
|
// 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;
|
|
string currSelection = "";
|
|
// 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);
|
|
}
|
|
#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>();
|
|
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
|
|
{ }
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public class iobAdapt
|
|
{
|
|
/// <summary>
|
|
/// Codice univoco macchina
|
|
/// </summary>
|
|
public string CodIOB { get; set; }
|
|
/// <summary>
|
|
/// Identificativo univoco processo
|
|
/// </summary>
|
|
public int pID { get; set; }
|
|
/// <summary>
|
|
/// DataOra avvio dell'IOB
|
|
/// </summary>
|
|
public DateTime startTime { get; set; }
|
|
/// <summary>
|
|
/// Verifica se il processo sia in RUN
|
|
/// </summary>
|
|
public bool isRunning { get; set; }
|
|
}
|
|
}
|