2016-04-06 19 views
0

Ich bin kein Profi WPF Benutzer, also kam ich hier, um Sie um Hilfe zu bitten. Ich möchte einige DB-Wert aktualisieren, wenn die Apps beenden, aber nichts passieren. hier ist mein app.xaml Code:WPF app.xaml Exit-Ereignis funktioniert nicht

<Application x:Class="pcAdmin.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:pcAdmin" 
      Startup="Application_Startup" Exit="Application_Exit" 
      > 
    <Application.Resources> 

    </Application.Resources> 
</Application> 

und hier ist der Code, hinter der Application_Exit Methode:

private void Application_Exit(object sender, ExitEventArgs e) 
      { 
//just for verification 
       System.Windows.Forms.MessageBox.Show("apps is shutting down"); 
//updating DB 
       conDB = new MySQLConnect(this.connexion_string); 
       conDB.connectDB(); 
       conDB.updateStatus("OFF", IP.mac); 
       conDB.disconnect(); 
       pw.requestStop(); 
      } 

NB: - kein Fehler mit allen fonction im application_exit sind, weil sie arbeiten gut in anderen Teil des Programms. - die apps hat kein window, so kann ich nicht windows_closing oder windows_closed event (aber versucht es mit ihnen und der Code funktioniert) Ich schätze Ihre Hilfe sehr. Danke

Hier ist der vollständige Code des app.xaml Code hinter

public partial class App : System.Windows.Application 
    { 
     private string pData; 
     private string pDataPath; 
     private NotifyIcon nIcon; 
     private string connexion_string; 
     private string host; 
     private string user; 
     private string pwd; 
     private string base_sql; 
     private InfoPC IP; 
     private MySQLConnect conDB; 
     private IPChangeDetect ipc; 
     private ProcessWorker pw; 
     private Programme programme; 
     private Service services; 
     private List<Svc> sc; 
     private AV antivirus; 
     private Processor processor; 

     private void Application_Startup(object sender, StartupEventArgs e) 
     { 
      this.pData = "%ALLUSERSPROFILE%"; 
      this.pDataPath = Environment.ExpandEnvironmentVariables(pData); 
      //chargement de l'icon dans la barre de tâche 
      nIcon = new NotifyIcon(); 
      this.nIcon.Icon = new Icon(@"D:\sav Jerrys\pro\getInfo2.0\pcAdmin\pcAdmin\logo.ico"); 
      nIcon.Visible = true; 
      nIcon.BalloonTipText = "Gathering PC info"; 
      nIcon.BalloonTipTitle = "AEthernam"; 
      nIcon.ShowBalloonTip(5000); 
      //mise à jour de l'application 
      getUpdated(); 
      //obtention des paramètres de connexion à la base de donnée 
      recupParam(); 
      //connexion à la base de donnée et envoi des information pendant le chargement de pcAdmin 
      connexion_string = "Server=" + this.host + ";Uid=" + this.user + ";password=" + this.pwd + ";database=" + this.base_sql + ";port=3306"; 
      this.IP = new InfoPC(); 
      IP.find_info(); 
      conDB = new MySQLConnect(connexion_string, IP.mac, IP.ip, IP.domaine, IP.nom); 
      conDB.connectDB(); 
      conDB.sendData(); 
      //detection du changement d'ip après le chargement du logiciel 
      ipc = new IPChangeDetect(conDB); 
      ipc.detectIPChange(); 
      conDB.disconnect(); 

      //THread for listing process 
      pw = new ProcessWorker(connexion_string, IP.mac, pDataPath + @"\AEthernam\proc.bin"); 
      Thread processThread = new Thread(pw.processWorker); 
      processThread.IsBackground = true; 
      processThread.Start(); 
      while (!processThread.IsAlive) ; 

      //récupération de la liste des programmes 
      recupProg(); 
      //récupération des services 
      recupService(); 
      //récupération AV 
      recupAV(); 
      //Recup info processeur 
      recupProcInfo(); 
      //TODO:Recup info disque dur physique 
      recupPhyDiskInfo(); 
      //TODO:Recup info partition 
      recupLogDiskInfo(); 
      //TODO:Recup info Mémoire 
      recupMemoryInfo(); 
     } 
     private void getUpdated() 
     { 

      // StreamWriter sw = new StreamWriter(pDataPath + "\\AEthernam\\aethernam.log"); 
      Version appsVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; 
      UpdateChecker upChecker = new UpdateChecker(appsVersion.ToString()); 
      upChecker.checkUpdate(); 
      //try 
      //{ 
      // sw.WriteLine(upChecker.getLog); 
      //} 
      //catch(Exception ex) 
      //{ 
      // this.log = "erreur ajout log dans le fichier log:\t" + ex.Message; 
      //} 
     } 

     private void recupParam() 
     { 
      try 
      { 
       StreamReader sr = new StreamReader(pDataPath + "\\AEthernam\\aethernam.cfg"); 
       string ligne = sr.ReadLine(); 
       this.host = ligne; 
       ligne = sr.ReadLine(); 
       this.user = ligne; 
       ligne = sr.ReadLine(); 
       this.pwd = ligne; 
       ligne = sr.ReadLine(); 
       this.base_sql = ligne; 
      } 
      catch (Exception ex) 
      { 
       System.Windows.MessageBox.Show(ex.Message); 
      } 
     } 
     private void recupProg() 
     { 
      programme = new Programme(); 
      List<prog> liste_programe = programme.recupProg(); 
      conDB = new MySQLConnect(connexion_string, IP.mac); 
      conDB.connectDB(); 
      if (System.IO.File.Exists(pDataPath + @"\AEthernam\prog.bin")) 
      { 
       List<prog> list_pro = programme.deserialize(pDataPath + @"\AEthernam\prog.bin"); 
       if (list_pro.Count != liste_programe.Count) 
       { 
        //mise à jour de la base des programmes 
        conDB.update_Prog(IP.mac, liste_programe); 
        conDB.disconnect(); 
        programme.serialize(list_pro, pDataPath + @"\AEthernam\prog.bin"); 
       } 
      } 
      else 
      { 
       conDB.sendProg(liste_programe); 
       conDB.disconnect(); 
       programme.serialize(liste_programe, pDataPath + @"\AEthernam\prog.bin"); 
      } 
     } 
     private void recupService() 
     { 
      this.services = new Service(); 
      sc = services.getSvc(); 
      conDB = new MySQLConnect(connexion_string, IP.mac); 
      conDB.connectDB(); 
      if (System.IO.File.Exists(pDataPath + @"\AEthernam\svc.bin")) 
      { 
       List<Svc> svcs = services.deserialize(pDataPath + @"\AEthernam\svc.bin"); 
       if (svcs.Count != sc.Count) 
       { 
        //Mise à jour de la base des sevices 
        conDB.updateService(IP.mac, svcs); 
        conDB.disconnect(); 
        services.serialize(svcs, pDataPath + @"\AEthernam\svc.bin"); 
       } 
      } 
      else 
      { 
       //insertion des services dans la base 
       conDB.sendService(sc); 
       conDB.disconnect(); 
       services.serialize(sc, pDataPath + @"\AEthernam\svc.bin"); 
      } 
     } 
     private void recupAV() 
     { 
      List<AV> listAv = new List<AV>(); 
      FindAv avInstallé = new FindAv(); 
      ManagementObjectCollection avs = avInstallé.AvInstalled(); 
      foreach (ManagementObject mo in avs) 
      { 
       antivirus = new AV(); 
       antivirus.nom = mo["DisplayName"].ToString(); 
       antivirus.etat = avInstallé.findState(mo["productState"].ToString()); 
       listAv.Add(antivirus); 
      } 
      conDB = new MySQLConnect(connexion_string, IP.mac); 
      conDB.connectDB(); 
      if (!System.IO.File.Exists(pDataPath + @"\AEthernam\prog.bin")) 
      { 
       foreach (AV av in listAv) 
       { 
        conDB.sendAV(av); 
        conDB.disconnect(); 

       } 

      } 
      else 
      { 
       conDB.updateAV(IP.mac, listAv); 
       conDB.disconnect(); 
      } 
     } 
     private void recupMemoryInfo() 
     { 
      List<Ram> liste_ram = new List<Ram>(); 
      liste_ram = new RamInfo().getRams(); 
      conDB = new MySQLConnect(connexion_string, IP.mac); 
      conDB.connectDB(); 
      if (!System.IO.File.Exists(pDataPath + @"\AEthernam\prog.bin")) 
      { 
       conDB.sendRam(liste_ram); 
       conDB.disconnect(); 
      } 
      else 
      { 
       conDB.updateRam(IP.mac, liste_ram); 
       conDB.disconnect(); 
      } 
     } 
     private void recupPhyDiskInfo() 
     { 
      List<DisquePhysique> liste_pdisk = new List<DisquePhysique>(); 
      liste_pdisk = new PhysicalDiskInfo().getPDisk(); 
      conDB = new MySQLConnect(connexion_string, IP.mac); 
      conDB.connectDB(); 
      if (!System.IO.File.Exists(pDataPath + @"\AEthernam\prog.bin")) 
      { 
       conDB.sendPhydisque(liste_pdisk); 
       conDB.disconnect(); 
      } 
      else 
      { 
       conDB.updatePhydisque(IP.mac, liste_pdisk); 
       conDB.disconnect(); 
      } 
     } 
     private void recupLogDiskInfo() 
     { 
      List<LogicalDisk> liste_ldisk = new List<LogicalDisk>(); 
      liste_ldisk = new LogicalDiskInfo().getPartition(); 
      conDB = new MySQLConnect(connexion_string, IP.mac); 
      conDB.connectDB(); 
      if (!System.IO.File.Exists(pDataPath + @"\AEthernam\prog.bin")) 
      { 
       conDB.sendLdisque(liste_ldisk); 
       conDB.disconnect(); 
      } 
      else 
      { 
       conDB.updateLdisque(IP.mac, liste_ldisk); 
       conDB.disconnect(); 
      } 
     } 
     private void recupProcInfo() 
     { 
      processor = new Processor(); 
      ProcessorInfo pInfo = new ProcessorInfo(); 
      processor = pInfo.getInfo(); 
      conDB = new MySQLConnect(connexion_string, IP.mac); 
      conDB.connectDB(); 
      if (!System.IO.File.Exists(pDataPath + @"\AEthernam\prog.bin")) 
      { 
       conDB.sendProcessor(processor); 
       conDB.disconnect(); 
      } 
      else 
      { 
       conDB.updateProcessor(IP.mac, processor); 
       conDB.disconnect(); 
      } 
     } 

     private void Application_Exit(object sender, ExitEventArgs e) 
     { 
      System.Windows.Forms.MessageBox.Show("apps is shutting down"); 
      conDB = new MySQLConnect(this.connexion_string); 
      conDB.connectDB(); 
      conDB.updateStatus("OFF", IP.mac); 
      conDB.disconnect(); 
      pw.requestStop(); 
     } 
    } 
+0

Welche Art von App ist das, wenn Sie kein Fenster haben? – Nitin

+0

Ändern Sie System.Windows.Forms.MessageBox.Show() zu MessageBox.SHow(), und warum verwenden Sie die Windows.Forms in einer WPF-Anwendung? –

+0

es ist zu überprüfen und bieten Informationen für die Netzwerküberwachung, so dass der Benutzer nicht mit ihm interagieren müssen, so dass ich jede Benutzeroberfläche basierend auf der Messabox deaktiviert wurde, ist nur zu Überprüfungszwecken. –

Antwort

0

ich ein Gefühl, das Problem ist mit dem Gewinde im Zusammenhang Sie haben. Wenn es sich um eine foreground thread handelt, kann die Anwendung nicht beendet werden, bis sie ausgeführt wird. Und da Sie versuchen, es in der Exit_Application-Methode zu stoppen, geschieht das nie. Versuchen Sie, irgendwo wo Sie den Thread erstellen zu setzen.

Wenn Sie neue Thread() verwenden, um es zu erstellen, ist es immer standardmäßig Vordergrund Thread.

UPDATE:

Da die Anwendung aus dem Task-Manager zu schließen, wird der Prozess einfach durch Fenster getötet, und es gibt keine Möglichkeit, einen Code auszuführen, bevor es geschlossen wird. Überprüfen Sie diese answer für eine Problemumgehung, die DB einbezieht. Oder Sie benötigen mindestens eine Benutzeroberfläche oder ein Taskleistensymbol, mit dem Sie die Anwendung bei einigen Tastenklicks elegant schließen können. Um anmutig die Anwendung schließen können Sie vorher vorgeschlagene Verwendung:

Application.Current.Shutdown(); 
+0

Vielen Dank für Ihre Antwort, ich habe meinen Code wie vorgeschlagen geändert, aber nicht funktioniert. Hier ist ein Beispielcode des Threads, der ausgeführt wurde –

+0

// THread für Auflistungsvorgang pw = neuer ProcessWorker (connexion_string, IP.mac, pDataPath + @ "\ AEthernam \ proc.bin"); Thread processThread = neuer Thread (pw.processWorker); processThread.IsBackground = true; processThread.Start(); while (! ProcessThread.IsAlive); –

+0

Das Problem ist, dass ich die apps nicht schließen muss, ich muss Daten an eine db sagen, dass der Benutzer die apps gestoppt hat :) in diesem Fall nur durch den Prozess zu töten –

0

Genau wie folgt vor:

private void Application_Startup(object sender, StartupEventArgs e) 
{ 
    //your current code 
    Application.Current.Shutdown(); 
} 

dies sollte die Exit-Methode Ihrer Anwendung auslösen und den Code innerhalb des application_exit laufen();

0

Vielen Dank für Ihre Antwort und Hilfe, ich werde @ 3615 Antwort als eine akzeptable Lösung für mich setzen. Ich werde gona einen Weg finden, um zu sehen, ob Benutzer Prozess getötet und dann meine DB entsprechend aktualisiert