Rebonjour:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Avancement_OFS
{
static class Program
{
static Process checker;
static Process main;
static int mainProcessID;
[STAThread]
static void Main(string[] args)
{
if (args.Length == 0)
{
//Saves current process info to pass on command line.
main = Process.GetCurrentProcess();
mainProcessID = main.Id;
//Initializes the helper process
checker = new Process();
checker.StartInfo.FileName = main.MainModule.FileName;
checker.StartInfo.Arguments = mainProcessID.ToString();
checker.EnableRaisingEvents = true;
checker.Exited += new EventHandler(checker_Exited);
//Launch the helper process.
checker.Start();
Application.Run(new Authentification());
}
else //On the helper Process
{
main = Process.GetProcessById(int.Parse(args[0]));
main.EnableRaisingEvents = true;
main.Exited += new EventHandler(main_Exited);
while (!main.HasExited)
{
Thread.Sleep(9000); //Wait 1 second.
}
//Provide some time to process the main_Exited event.
Thread.Sleep(10000);
}
}
static void checker_Exited(object sender, EventArgs e)
{
if (Process.GetProcessesByName("taskmgr").Length != 0)
{
SqlConnection cnx_save_trace = new SqlConnection(" Data Source=***;Initial Catalog=OFS_DEV;Integrated Security=True");
SqlCommand cmd_save = new SqlCommand("insert into SUIVI_ACTIVITE_USER([USER_NAME],[DT])values(@name,@dt_s)", cnx_save_trace);
cnx_save_trace.Open();
cmd_save.Parameters.AddWithValue("@name",user);//Recuperer de la form Authentification
cmd_save.Parameters.AddWithValue("@dt_s", DateTime.Now);
cmd_save.ExecuteNonQuery();
cnx_save_trace.Close();
//MessageBox.Show("Task Manager killed helper process.");
//If you like you could kill the main app here to.
//main.Kill();
}
}
static void main_Exited(object sender, EventArgs e)
{
if (Process.GetProcessesByName("taskmgr").Length != 0)
{
SqlConnection cnx_save_trace = new SqlConnection(" Data Source=***;Initial Catalog=OFS_DEV;Integrated Security=True");
SqlCommand cmd_save = new SqlCommand("insert into SUIVI_ACTIVITE_USER([USER_NAME],[DT])values(@name,@dt_s)", cnx_save_trace);
cnx_save_trace.Open();
cmd_save.Parameters.AddWithValue("@name",user);//Recuperer de la form Authentification
cmd_save.Parameters.AddWithValue("@dt_s", DateTime.Now);
cmd_save.ExecuteNonQuery();
cnx_save_trace.Close();
}
}
}
}