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?