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

C# Discussion :

Besoin d'aide sur l'optimisation d'une application


Sujet :

C#

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 5
    Points : 6
    Points
    6
    Par défaut Besoin d'aide sur l'optimisation d'une application
    Bonsoir,

    voilà je débute en programmation c# et j'ai fait mon premier programme, qui n'est pas du tout optimisé et est surement bourré d'erreur de code
    (soyez indulgent les puristes :-) )
    Le programme en question est un programme en console qui prend en argument 2 paramètres (local_path, usb_path)

    Le programme se lance de lui même en fond de tache et se ferme automatiquement
    possède un timer qui répète une fonction de copy toutes les 1000ms
    J'ai bien coder un bout mais comme je l'ai déjà dit, il y a de gros problème d'optimisation. Je cherche quelqu'un qui sera en mesure de critiquer mon code (intelligemment) et me diriger vers la bonne direction. Par example comment utiliser un Timer sans utiliser cette boucle ...

    le code en question :

    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
    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
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Timers;
    using System.Diagnostics;
    using System.IO;
     
    namespace ShadowCopy
    {
        class Program
        {
            static public bool is_a_copy;
            static public string s_usb_path;
            static public string s_local_path;
     
            static void Main(string[] args)
            {
                is_a_copy = false;
                if ( args.Count() < 2 )
                {
                    Console.WriteLine("Usage : Shadowcopy.exe [OPTIONS] chemin vers le disque local ... chemin vers la clef usb ... ");
                    Environment.Exit(0);
                }
                s_local_path= args[0];
                s_usb_path = args[1];
                if (args.Count() == 3)
                {  
                    bool result = args[2].Equals("--HIDE", StringComparison.Ordinal);
                    if (result)
                    is_a_copy = true;
                }
     
     
                Timer myTimer = new Timer();
                myTimer.Elapsed += new ElapsedEventHandler(StartTimerEvent);
                myTimer.Interval = 1000;
                myTimer.Start();
     
                while ( Console.Read() != 'q' )
                {
                    ;    // do nothing...
                }
            }
     
            public static bool StartSc(bool is_a_copy, string local_path, string usb_path)
            {
                if (!is_a_copy)
                {
                    Process ShadowProcess = new Process();
                    ShadowProcess.StartInfo.FileName = System.Reflection.Assembly.GetExecutingAssembly().Location;
                    ShadowProcess.StartInfo.Arguments = local_path + " " + usb_path + " --HIDE ";
                    ShadowProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    if(ShadowProcess.Start() == true)
                        Environment.Exit(0);
                    else
                        Console.Write("Error");
                }
                else
                {
                    try
                    {
                        Directory.CreateDirectory(usb_path);
                    }
                    catch
                    {
                        ;//
                    }
     
                    try
                    {
                        foreach(var file in Directory.GetFiles(local_path))
                            File.Copy(file, Path.Combine(usb_path, Path.GetFileName(file)));
                    }
                    catch
                    {
                        ;//
                    }
     
                    try
                    {
                            foreach (var directory in Directory.GetDirectories(local_path))
                            StartSc(true, directory, Path.Combine(usb_path, Path.GetFileName(directory)));
                    }
                    catch
                    {
                        ;//
                    }
     
                }
                return true;
            }
     
            public static void StartTimerEvent(object source, ElapsedEventArgs e)
            {
                StartSc(is_a_copy, s_local_path, s_usb_path);
            }
        }
    }

  2. #2
    Membre expérimenté
    Avatar de charouel
    Homme Profil pro
    Freelance
    Inscrit en
    Mars 2009
    Messages
    618
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2009
    Messages : 618
    Points : 1 454
    Points
    1 454
    Billets dans le blog
    9
    Par défaut
    *Je te conseil de faire un service Windwos qui tourne en background au lieu de timer et boucle

  3. #3
    Expert confirmé

    Homme Profil pro
    Responsable déploiement (SCCM, InTune, GPO)
    Inscrit en
    Juillet 2014
    Messages
    3 196
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Responsable déploiement (SCCM, InTune, GPO)
    Secteur : Transports

    Informations forums :
    Inscription : Juillet 2014
    Messages : 3 196
    Points : 5 785
    Points
    5 785
    Par défaut
    Voici quelques premières modifications que j’effectuerai sur le code.
    D’ailleurs je ne comprend pas exactement ce que doit faire ce code et comment il doit le faire.
    is_a_copy ?
    Timer ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
                if (args.Count() == 3 && args[2].Equals("--HIDE", StringComparison.Ordinal))
                    is_a_copy = true;
    Remplacer la fonction (StartSc) de copie des dossiers/fichiers par une fonction plus "propre" :
    Code trouvé sur MSDN https://msdn.microsoft.com/en-us/lib...ctoryinfo.aspx

    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
        public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
        {
            if (source.FullName.ToLower() == target.FullName.ToLower())
            {
                return;
            }
     
            // Check if the target directory exists, if not, create it.
            if (Directory.Exists(target.FullName) == false)
            {
                Directory.CreateDirectory(target.FullName);
            }
     
            // Copy each file into it's new directory.
            foreach (FileInfo fi in source.GetFiles())
            {
                Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
                fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
            }
     
            // Copy each subdirectory using recursion.
            foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
            {
                DirectoryInfo nextTargetSubDir =
                    target.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir);
            }
        }

Discussions similaires

  1. Besoin d'aide sur la création d'une page Web
    Par FournelAlex dans le forum Général Conception Web
    Réponses: 2
    Dernier message: 21/01/2011, 17h37
  2. [Projet tech] Besoin d'aide sur le fonctionnel d'une appli
    Par FinalSpirit dans le forum Etudes
    Réponses: 0
    Dernier message: 23/11/2009, 12h56
  3. Réponses: 5
    Dernier message: 08/04/2009, 17h39
  4. [SQL] Besoin d'aide sur les attributs pour une requete
    Par bobobobo01 dans le forum Langage SQL
    Réponses: 2
    Dernier message: 27/11/2006, 21h39
  5. [C#] Besoin d'aide sur l'affichage d'une combobox
    Par dcd3 dans le forum Windows Forms
    Réponses: 2
    Dernier message: 08/10/2005, 00h43

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