WPF app.xaml退出事件无法正常工作

时间:2016-04-06 06:36:58

标签: c# wpf exit app.xaml

我不是专业的用户,所以我来这里是为了寻求帮助。 我想在应用程序退出时更新某些数据库值,但没有任何反应。这是我的app.xaml代码:

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

以下是Application_Exit方法的代码:

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

注意: - application_exit中的所有功能都没有错误,因为它们在程序的其他部分工作正常。     - 应用程序没有窗口,所以我不能使用windows_closing或windows_closed事件(但尝试使用它们和代码工作) 我非常感谢你的帮助。谢谢

以下是

背后的app.xaml代码的完整代码
 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();
        }
    }

3 个答案:

答案 0 :(得分:0)

我感觉问题与您拥有的主题有关。如果它是foreground thread,则它不允许应用程序退出,直到它运行。因为你试图在Exit_Application方法中停止它,所以从未发生过。 尝试设置创建线程的位置。

Application.Current.Shutdown();

如果您使用新的Thread()来创建它,它总是默认为前台线程。

更新:

由于应用程序正在从任务管理器关闭,因此该过程只是被Windows杀死,并且在关闭之前无法执行某些代码。检查此answer以获取解决方法,包括数据库。 或者至少需要一些UI或托盘图标,这样可以让您在点击某个按钮时优雅地关闭应用程序。 要优雅地关闭应用程序,您可以使用之前的建议:

print("line one\nline two\nline three")

答案 1 :(得分:0)

请执行以下操作:

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

这应该触发应用程序的Exit方法并在application_exit();

中运行代码

答案 2 :(得分:0)

谢谢大家的回答和帮助,我将@ 3615答案作为可接受的解决方案。我会找到一些方法来查看用户是否已杀死进程然后相应地更新我的数据库