218 lines
5.3 KiB
C#
218 lines
5.3 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// Elenco dei processi
|
|
/// </summary>
|
|
protected Dictionary<string, iobAdapt> childFormList;
|
|
|
|
public IOBManPanel()
|
|
{
|
|
InitializeComponent();
|
|
myInit();
|
|
}
|
|
|
|
private void myInit()
|
|
{
|
|
childFormList = new Dictionary<string, iobAdapt>();
|
|
MainTimer.Start();
|
|
}
|
|
|
|
/// <summary>
|
|
/// apro eseguibile dump
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
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);
|
|
}
|
|
|
|
/// <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();
|
|
}
|
|
|
|
private void chiudiChild()
|
|
{
|
|
// recupero elenco processi (se almeno 1)
|
|
if (childFormList.Count > 0)
|
|
{
|
|
KeyValuePair<string, iobAdapt> 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
|
|
}
|
|
/// <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
|
|
{ }
|
|
}
|
|
}
|
|
|
|
private void btnStartChild_Click(object sender, EventArgs e)
|
|
{
|
|
CForm child = new CForm();
|
|
child.MdiParent = this;
|
|
child.Show();
|
|
|
|
this.LayoutMdi(MdiLayout.TileHorizontal);
|
|
}
|
|
}
|
|
|
|
|
|
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; }
|
|
}
|
|
}
|