IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

Application.Restart ne fonctionne plus


Sujet :

Windows Forms

  1. #1
    Membre habitué Avatar de Ishizaki
    Inscrit en
    Avril 2006
    Messages
    262
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 262
    Points : 175
    Points
    175
    Par défaut Application.Restart ne fonctionne plus
    Bonjour !

    J'ai mis en place une gestion de versioning avec ClickOnce, ça marchait nickel jusqu'au moment où j'ai du gérer les instances d'application, c'est à dire, ne pouvoir lancer qu'une seule instance de l'application.

    Quand mon Update est fini:

    MainForm.cs :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    /// <summary>
            /// Evènement sur l'update finalisé de la version
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void applicationDeployment_UpdateCompleted(object sender, AsyncCompletedEventArgs e)
            {
     
                    this.Close();
                    Application.Restart();
     
            }
    Donc ceci marchait très bien.
    Note: J'ai mis un this.Close(); avant le Application.Restart();, car quand je relançais l'appli avec un restart(); seulement, il ne me quittait pas l'instance précédente -___-.

    Pour gérer la condition que l'appli ne pouvait avoir qu'une instance en cours, j'ai mis en place une gestion de Process (codes trouvés sur le web).

    Program.cs :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                if (Program.IfProcessRunning() == false)
                {
                     //Code autre...
                    Application.Run(new FormMain());
                }
     
            }
     
            /// <summary>
            /// Détecte si une application est déjà lancée et stoppe le lancement d'une nouvelle
            /// </summary>
            /// <returns></returns>
            public static bool IfProcessRunning()
            {
     
                Process processThis = Process.GetCurrentProcess();
     
                Process[] processesArray = Process.GetProcesses();
     
                foreach (Process process in processesArray)
                {
                    if (processThis.Id != process.Id)
                    {
                        if (processThis.ProcessName == process.ProcessName)
                        {
                            return true;
     
                        }
     
                    }
                }
     
                return false;
     
     
            }
     
        }
    En bref, comment conserver cette gestion de redémarrage après un Update de version sans avoir de doublons d'instances...

    Merci de votre aide !

  2. #2
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    27
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations forums :
    Inscription : Décembre 2006
    Messages : 27
    Points : 33
    Points
    33
    Par défaut
    En regardant un peu avec réflector, il semblerait que l'arrêt du processus se fait déjà dans Application.Restart :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    public static void Restart()
    {
        if (Assembly.GetEntryAssembly() == null)
        {
            throw new NotSupportedException(SR.GetString("RestartNotSupported"));
        }
        bool flag = false;
        Process currentProcess = Process.GetCurrentProcess();
        if (string.Equals(currentProcess.MainModule.ModuleName, "ieexec.exe", StringComparison.OrdinalIgnoreCase))
        {
            string directoryName = string.Empty;
            new FileIOPermission(PermissionState.Unrestricted).Assert();
            try
            {
                directoryName = Path.GetDirectoryName(typeof(object).Module.FullyQualifiedName);
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
            if (string.Equals(directoryName + @"\ieexec.exe", currentProcess.MainModule.FileName, StringComparison.OrdinalIgnoreCase))
            {
                flag = true;
                ExitInternal();
                string data = AppDomain.CurrentDomain.GetData("APP_LAUNCH_URL") as string;
                if (data != null)
                {
                    Process.Start(currentProcess.MainModule.FileName, data);
                }
            }
        }
        if (!flag)
        {
            if (ApplicationDeployment.IsNetworkDeployed)
            {
                string updatedApplicationFullName = ApplicationDeployment.CurrentDeployment.UpdatedApplicationFullName;
                uint hostTypeFromMetaData = (uint) ClickOnceUtility.GetHostTypeFromMetaData(updatedApplicationFullName);
                ExitInternal();
                UnsafeNativeMethods.CorLaunchApplication(hostTypeFromMetaData, updatedApplicationFullName, 0, null, 0, null, new UnsafeNativeMethods.PROCESS_INFORMATION());
            }
            else
            {
                string[] commandLineArgs = Environment.GetCommandLineArgs();
                StringBuilder builder = new StringBuilder((commandLineArgs.Length - 1) * 0x10);
                for (int i = 1; i < (commandLineArgs.Length - 1); i++)
                {
                    builder.Append('"');
                    builder.Append(commandLineArgs[i]);
                    builder.Append("\" ");
                }
                if (commandLineArgs.Length > 1)
                {
                    builder.Append('"');
                    builder.Append(commandLineArgs[commandLineArgs.Length - 1]);
                    builder.Append('"');
                }
                ProcessStartInfo startInfo = Process.GetCurrentProcess().StartInfo;
                startInfo.FileName = ExecutablePath;
                if (builder.Length > 0)
                {
                    startInfo.Arguments = builder.ToString();
                }
                ExitInternal();
                Process.Start(startInfo);
            }
        }
    }
    Donc avant de mettre un dans ton code, je t'invite à plutôt chercher à savoir pourquoi l'application ne se ferme pas quand tu fais un Application.Restart(); Tu as peut être un problème de threading ....

  3. #3
    Membre habitué Avatar de Ishizaki
    Inscrit en
    Avril 2006
    Messages
    262
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 262
    Points : 175
    Points
    175
    Par défaut
    Bien vu, problème de Thread en effet... le redémarrage se fait tellement rapidement qu'il capte 2 process en même temps donc... Plus de redémarrage.

  4. #4
    Membre habitué Avatar de Ishizaki
    Inscrit en
    Avril 2006
    Messages
    262
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 262
    Points : 175
    Points
    175
    Par défaut
    J'ai modifié le code comme suit :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    if (Program.IfProcessRunning() == false)
                {
                    System.Threading.Thread.Sleep(500);
     
                    if (Program.IfProcessRunning() == false)
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new FormMain());
                    }
                }
    Mais rien n'y fait, il ne veut pas redémarrer...

  5. #5
    Membre habitué Avatar de Ishizaki
    Inscrit en
    Avril 2006
    Messages
    262
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 262
    Points : 175
    Points
    175
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     
    if (Program.IfProcessRunning() == true)
                {
                    System.Threading.Thread.Sleep(500);
     
                    if (Program.IfProcessRunning() == true)
                    {
                        Application.Exit();
                    }
                    else
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new FormMain());
                    }
                }
                else
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new FormMain());
                }

    Ca ça marche mais c'est moche...

    Edit: J'ai gardé cette solution, je vais l'améliorer un peu.

    Merci de votre aide .

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Application Java ne fonctionne plus
    Par Zenjïn dans le forum Général Java
    Réponses: 4
    Dernier message: 09/04/2015, 14h07
  2. Mon application Android ne fonctionne plus
    Par android84 dans le forum Android
    Réponses: 2
    Dernier message: 16/09/2011, 20h45
  3. Réponses: 1
    Dernier message: 15/05/2010, 09h50
  4. Application.Restart() ne fonctionne pas
    Par xav2303 dans le forum Windows Forms
    Réponses: 6
    Dernier message: 11/08/2009, 16h20
  5. Réponses: 0
    Dernier message: 15/07/2009, 15h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo