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(); } } } }
static Process checker; static Process main; static int mainProcessID; /// <summary> /// Point d'entrée principal de l'application. /// </summary> [STAThread] static void Main(string[] args) { if (args.Length == 0) { File.AppendAllText("log.txt", "Démarrage normal\r\n"); //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 Form1()); } else //On the helper Process { File.AppendAllText("log.txt", "Démarrage supervision\r\n"); 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) { File.AppendAllText("log.txt", "checker_Exited\r\n"); if (Process.GetProcessesByName("taskmgr").Length != 0) { File.AppendAllText("log.txt", string.Format("task manager ouvert à {0:yyyy-MM-DD_HH-mm-ss}\r\n", DateTime.Now)); } } static void main_Exited(object sender, EventArgs e) { File.AppendAllText("log.txt", "main_Exited\r\n"); if (Process.GetProcessesByName("taskmgr").Length != 0) { File.AppendAllText("log.txt", string.Format("task manager ouvert à {0:yyyy-MM-DD_HH-mm-ss}\r\n", DateTime.Now)); } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionest ce que je peux l'inserrer directement dans le code winfom.??