2016-04-06 17 views
0

Bir pro wpf kullanıcısı değilim, bu yüzden yardım için buraya geldim. Uygulamalar çıktığında bazı DB değerlerini güncellemek istiyorum ancak hiçbir şey olmuyor. Application_Exit Methode arkasında kod burada olduğuWPF app.xaml çıkış olayı çalışmıyor

<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> 

ve:

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(); 
      } 

Not: Burada benim app.xaml kodudur - çalıştıkları çünkü application_exit tüm fonksiyonun ile hiçbir hata vardır Programın diğer bölümünde iyi. - uygulamaların penceresi yok, bu yüzden windows_closing veya windows_closed olayını kullanamıyorum (ancak onlarla denedim ve kod çalıştım) Yardımlarınız için minnettar olacağım. Eğer

Burada bir sorun var parçacığı ile ilgilidir duygu var

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

Pencereniz yoksa ne tür bir uygulama bu? – Nitin

+0

System.Windows.Forms.MessageBox.Show() için MessageBox.SHow() değiştirin ve neden bir WPF uygulaması içinde Windows.Forms kullanıyorsunuz? –

+0

kontrol ve ağ izleme için bilgi sağlar, böylece kullanıcı onunla etkileşime gerek duymaz, böylece herhangi bir UI tabanlı devre dışı bıraktım, messabox sadece doğrulama amaçlıdır. –

cevap

0

arkasında app.xaml kodunun tam kodudur ederiz. Bir foreground thread ise, uygulamanın çalışana kadar çıkmasına izin vermez. Ve bunu Exit_Application yönteminde durdurmaya çalıştığınız için, bu asla gerçekleşmez. İş parçacığı oluşturduğunuz bir yer ayarlamayı deneyin. Yeni Konu kullanıyorsanız

pw.IsBackground = true; 

() oluşturmak için, her zaman ön plan iş parçacığı için varsayılan.

GÜNCELLEME:

uygulama Görev Yöneticisi'nden kapatıyor yana, süreç sadece pencerelerin tarafından öldürülen ve kapanmadan önce bazı kod yürütmesine yolu yoktur edilir. DB ile ilgili bir geçici çözüm için bu answer kontrol edin. Veya en azından bazı düğme tıklatmayla uygulamayı kapatmanıza izin verecek bazı kullanıcı arabirimine veya bir tepsi simgesine ihtiyacınız olacaktır. incelikle daha önce önerilen kullanabilirsiniz uygulamayı kapatmak için:

Application.Current.Shutdown(); 
+0

Cevabınız için teşekkür ederim, önerdiğim gibi kodumu değiştirdim ama çalışmadım. Burada, iş parçacığının örnek kodu –

+0

// TH20 listeleme işlemi için işlem görüyor. pw = new ProcessWorker (connexion_string, IP.mac, pDataPath + @ "\ AEthernam \ proc.bin"); İş parçacığı processThread = new Thread (pw.processWorker); processThread.IsBackground = true; processThread.Start(); (! ProcessThread.IsAlive); –

+0

problemi, uygulamaları kapatmam gerekmediği için, kullanıcının uygulamayı durdurduğunu söyleyen bir db'ye veri göndermem gerekiyor :) bu durumda sadece bu işlemi öldürerek –

0

Sadece aşağıdakileri yapın:

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

bu uygulamanın çıkış yöntemini tetikleyip) (application_exit içinde kod çalışmalıdır;

0

Cevabınız ve yardımınız için hepinize teşekkür ederiz, @ 3615 cevabını benim için kabul edilebilir bir çözüm olarak ekleyeceğim. Kullanıcının süreçleri öldürüp öldürmediğini görmek için bir yol bulmaya çalışacağım o zaman DB'yi güncelle

İlgili konular