433 lines
11 KiB
C#
433 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Windows.Forms;
|
|
|
|
namespace IOB_MAN
|
|
{
|
|
|
|
|
|
|
|
public partial class IOBManPanel : Form
|
|
{
|
|
#region area gestione hode/max finestre
|
|
|
|
private const int SW_SHOWNORMAL = 1;
|
|
private const int SW_SHOWMINIMIZED = 2;
|
|
private const int SW_SHOWMAXIMIZED = 3;
|
|
[DllImport("user32.dll")]
|
|
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
|
|
|
|
#endregion
|
|
|
|
protected int countCheck = utils.CRI("chekMult");
|
|
/// <summary>
|
|
/// Elenco ARGS (uno per child da avviare)
|
|
/// </summary>
|
|
public List<string> ArgsList = new List<string>();
|
|
/// <summary>
|
|
/// Binding source degli elementi gestiti..
|
|
/// </summary>
|
|
private BindingSource ElencoIOB = new BindingSource();
|
|
/// <summary>
|
|
/// Path dell'exe da chiamare
|
|
/// </summary>
|
|
protected string TargetExe = "";
|
|
|
|
public IOBManPanel()
|
|
{
|
|
InitializeComponent();
|
|
myInit();
|
|
updateStatus();
|
|
}
|
|
|
|
private void myInit()
|
|
{
|
|
utils.lgInfo("Starting App");
|
|
// init prog bar
|
|
tsProgBar.Maximum = 100;
|
|
tsProgBar.Step = 1;
|
|
// gestione eventi binding source
|
|
ElencoIOB.AddingNew += ElencoIOB_AddingNew;
|
|
ElencoIOB.ListChanged += ElencoIOB_ListChanged;
|
|
// colelgo tab a binding
|
|
dgvManagedItems.DataSource = ElencoIOB;
|
|
MainTimer.Interval = utils.CRI("checkPeriod");
|
|
TargetExe = utils.CRS("targetExe");
|
|
if (string.IsNullOrEmpty(TargetExe))
|
|
{
|
|
TargetExe = string.Format(@"{0}\Test.bat", Application.StartupPath);
|
|
}
|
|
utils.lgInfo($"Target exe: {TargetExe}");
|
|
string ArgsString = utils.CRS("ArgsList");
|
|
utils.lgInfo($"Args found: {ArgsString}");
|
|
if (string.IsNullOrEmpty(ArgsString))
|
|
{
|
|
var rand = new Random();
|
|
// ne creo rand (5-15) di default...
|
|
for (int i = 0; i < rand.Next(5, 15); i++)
|
|
{
|
|
ArgsList.Add("127.0.0.1");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var elenco = ArgsString.Split(',');
|
|
foreach (var item in elenco)
|
|
{
|
|
ArgsList.Add(item);
|
|
}
|
|
}
|
|
MainTimer.Start();
|
|
utils.lgInfo("Timer started");
|
|
}
|
|
|
|
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
|
|
tsslNumProc.Text = $"Avviati {ElencoIOB.Count} processi child";
|
|
}
|
|
/// <summary>
|
|
/// Chiama apertura + update status...
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void btnOpen_Click(object sender, EventArgs e)
|
|
{
|
|
apriChild();
|
|
updateStatus();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Apro un child x ogni args specificato
|
|
/// </summary>
|
|
private void apriChild()
|
|
{
|
|
//Process childProc = new Process();
|
|
ProcessStartInfo psi = null;
|
|
foreach (var item in ArgsList)
|
|
{
|
|
// da testare x aprire chiudere risorsa...
|
|
psi = new ProcessStartInfo
|
|
{
|
|
FileName = TargetExe,
|
|
Arguments = item,
|
|
WindowStyle = ProcessWindowStyle.Minimized
|
|
};
|
|
|
|
//childProc.StartInfo = psi;
|
|
Process p = Process.Start(psi);
|
|
|
|
// accodo nuovo IOB...
|
|
iobAdapt newIob = new iobAdapt();
|
|
DateTime adesso = DateTime.Now;
|
|
newIob.CodIOB = item;
|
|
newIob.startTime = adesso;
|
|
newIob.pID = p.Id;
|
|
newIob.isRunning = true;
|
|
// aggiungo a datasource
|
|
ElencoIOB.Add(newIob);
|
|
|
|
utils.lgInfo($"Avviato child process per {item} | pid: {p.Id}");
|
|
}
|
|
|
|
|
|
#if false
|
|
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);
|
|
#endif
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Cerca nell'elenco il processo corrente
|
|
/// </summary>
|
|
/// <param name="processlist"></param>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public Process myGetProcByID(Process[] processlist, int id)
|
|
{
|
|
return processlist.FirstOrDefault(pr => pr.Id == id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua tutte le verifiche periodiche...
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void MainTimer_Tick(object sender, EventArgs e)
|
|
{
|
|
updateProgBar();
|
|
countCheck--;
|
|
if (countCheck < 0)
|
|
{
|
|
countCheck = utils.CRI("chekMult");
|
|
// se scaduto timer...
|
|
checkRunningchild();
|
|
showUpdate();
|
|
}
|
|
}
|
|
|
|
private void updateProgBar()
|
|
{
|
|
tsProgBar.PerformStep();
|
|
if (tsProgBar.Value >= tsProgBar.Maximum)
|
|
{
|
|
tsProgBar.Value = 0;
|
|
}
|
|
}
|
|
|
|
/// <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;
|
|
|
|
// leggo 1 sola volta TUTTO elenco processi...
|
|
Process[] processList = Process.GetProcesses();
|
|
|
|
foreach (iobAdapt item in ElencoIOB.List)
|
|
{
|
|
// verifico se esista il processo...
|
|
try
|
|
{
|
|
Process p = myGetProcByID(processList, item.pID);
|
|
needRem = p.HasExited;
|
|
}
|
|
catch
|
|
{
|
|
needRem = true;
|
|
}
|
|
if (needRem)
|
|
{
|
|
proc2rem.Add(item.pID);
|
|
item2rem.Add(item);
|
|
}
|
|
}
|
|
|
|
// ora procdedo alla cancellazione...
|
|
foreach (var item in item2rem)
|
|
{
|
|
ElencoIOB.Remove(item);
|
|
}
|
|
// aggiorno datagrid!
|
|
dgvManagedItems.Invalidate();
|
|
}
|
|
|
|
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();
|
|
apriChild();
|
|
}
|
|
|
|
private void lblNumChild_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void IOBManPanel_Load_1(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
private void dgvManagedItems_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
|
|
{
|
|
// seleziono riga...
|
|
int pid = -1;
|
|
if (e.RowIndex >= 0)
|
|
{
|
|
//dgvManagedItems.CurrentCell = dgvManagedItems.Rows[e.RowIndex].Cells[0];
|
|
|
|
dgvManagedItems.Rows[e.RowIndex].Selected = true;
|
|
using (var riga = dgvManagedItems.Rows[e.RowIndex])
|
|
{
|
|
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...
|
|
var windowsHandle = p.MainWindowHandle;
|
|
ShowWindowAsync(windowsHandle, SW_SHOWNORMAL);
|
|
}
|
|
catch
|
|
{
|
|
// errore era già chiuso..
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnMinimizeAll_Click(object sender, EventArgs e)
|
|
{
|
|
foreach (iobAdapt item in ElencoIOB.List)
|
|
{
|
|
try
|
|
{
|
|
Process p = Process.GetProcessById(item.pID);
|
|
// cerco e chiudo quelli che mi interessano...
|
|
var windowsHandle = p.MainWindowHandle;
|
|
ShowWindowAsync(windowsHandle, SW_SHOWMINIMIZED);
|
|
}
|
|
catch
|
|
{
|
|
// errore era già chiuso..
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnMaximixeAll_Click(object sender, EventArgs e)
|
|
{
|
|
foreach (iobAdapt item in ElencoIOB.List)
|
|
{
|
|
try
|
|
{
|
|
Process p = Process.GetProcessById(item.pID);
|
|
// cerco e chiudo quelli che mi interessano...
|
|
var windowsHandle = p.MainWindowHandle;
|
|
ShowWindowAsync(windowsHandle, SW_SHOWNORMAL);
|
|
}
|
|
catch
|
|
{
|
|
// errore era già chiuso..
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|