1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| 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
{
/// Point d'entrée principal de l'application.
/// </summary>
///
static Process checker;
static Process main;
static int mainProcessID;
static string user = "";
[STAThread]
public static string SeparateName(string fullName)
{
string[] wordsInText = fullName.Split(' ');
return wordsInText[0];
}
static void Main(string[] args)
{
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Authentification());
//Main App Process.
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)
{
//This only checks for the task manager process running.
//It does not make sure that the app has been closed by it. But close enough.
//If you can think of a better way please let me know.
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 user du formulaire 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)
{
//This only checks for the task manager process running.
//It does not make sure that the app has been closed by it. But close enough.
//If you can think of a better way please let me know.
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 user du formulaire Authentification
cmd_save.Parameters.AddWithValue("@dt_s", DateTime.Now);
cmd_save.ExecuteNonQuery();
cnx_save_trace.Close();
}
}
}
} |
Partager