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

Services Windows Discussion :

Service Windows Timer + Rename File


Sujet :

Services Windows

  1. #1
    Membre à l'essai
    Femme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2012
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Juillet 2012
    Messages : 10
    Points : 10
    Points
    10
    Par défaut Service Windows Timer + Rename File
    Bonjour,

    J'ai un problème avec mon service!

    J'explique le contexte:

    Service windows avec un timer configurer a tout les heures

    Je vérifie si le fichier A existe
    Si il existe je veux le renommer

    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
     
     protected override void OnStart(string[] args)
            {
               try
                {
                    Log.WriteLogInFile("ONSTART", "Begining start", xLogfile);
     
     
     
                    TimerCallback tmrCallBack = new TimerCallback(oTimer_TimerCallback);
                    oTimer = new Timer(tmrCallBack);
                    // have the time start in 1 second, and then fire once every hour
                    oTimer.Change(new TimeSpan(0, 0, 1), new TimeSpan(0, xExportFrequency, 0));
                }
                catch (Exception ex)
                {
                    Log.WriteLogInFile("EXCEPTION ONSTART", ex.Message, xLogfile);
     
                }
            }
    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
       private void oTimer_TimerCallback(object state)
            {
                //Manually stop the timer...
                oTimer.Change(Timeout.Infinite, Timeout.Infinite);
                // do what ever needs to be done.
                if (!((DateTime.Now.TimeOfDay >= xStartNightAudit.TimeOfDay) && (DateTime.Now.TimeOfDay <= xStopNightAudit.TimeOfDay)))
                {
                    if ((File.Exists(FileRental)) && (WorkFile==""))
                    {
                        WorkFile = xPath + (Path.GetFileNameWithoutExtension(FileRental)) + DateTime.Now.ToString("yyyyMMddhhmmss") +(Path.GetExtension(FileRental));
     
                        File.Move(FileRental, WorkFile);
                        Log.WriteLogInFile("*************** START WORK ON FILE FAC RENTAL ***************", FileRental, xLogfile);
                        addtofiletext(0);
                        Log.WriteLogInFile("*************** END WORK ON FILE FAC RENTAL ***************", FileRental, xLogfile);
                    }
                    if ((File.Exists(FileConfirmation)) && (WorkFile == ""))
                    {
                        WorkFile = xPath + (Path.GetFileNameWithoutExtension(FileConfirmation)) + DateTime.Now.ToString("yyyyMMddhhmmss") + (Path.GetExtension(FileConfirmation));
                        File.Move(FileConfirmation, WorkFile);
                        Log.WriteLogInFile("*************** START WORK ON FILE SPI CONFIRMATION ***************", FileConfirmation, xLogfile);
                        addtofiletext(1);
                        Log.WriteLogInFile("*************** END WORK ON FILE SPI CONFIRMATION ***************", FileConfirmation, xLogfile);
                    }
                    if ((ExportFileName == null) && (WorkFile == ""))
                    {
                        Log.WriteLogInFile("*************** START WORK TO GET RESERVATION MODIFICATION ***************", "", xLogfile);
                        GetInsertUpdate();
                        Log.WriteLogInFile("*************** END WORK TO GET RESERVATION MODIFICATION ***************", "", xLogfile);
                    }
     
                }
                // have the time start in 1 minute, and then fire once every hour
                oTimer.Change(new TimeSpan(0, 1, 0), new TimeSpan(0, xExportFrequency, 0));
            }
    J'ai toujours l'exception The process cannot access the file because it is being used by another process sur le rename

    Vous avez une idée de comment je peux contourner ce problème?

  2. #2
    Membre actif
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Septembre 2012
    Messages
    170
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Septembre 2012
    Messages : 170
    Points : 234
    Points
    234
    Par défaut
    Bonjour,
    tu peux essayer cette approche des timers pour commencer puis la customiser un peu plus:
    http://jormes.developpez.com/article...indows-dotnet/
    Bon courage

Discussions similaires

  1. [C#] Service Windows & Timer
    Par Iloyo dans le forum Services Windows
    Réponses: 2
    Dernier message: 02/11/2010, 13h13
  2. [C# .Net 1.1] Service Windows et timer
    Par plasticgoat dans le forum C#
    Réponses: 2
    Dernier message: 11/02/2009, 15h46
  3. Timer inopérant dans service windows
    Par pdgnr dans le forum Windows Forms
    Réponses: 7
    Dernier message: 01/05/2008, 16h38
  4. service windows et timer
    Par migutz dans le forum VB.NET
    Réponses: 3
    Dernier message: 18/03/2008, 11h58
  5. [C#][service windows] problème de débutant avec 1 timer
    Par Nycos62 dans le forum Windows Forms
    Réponses: 3
    Dernier message: 14/10/2005, 12h22

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