I created a service that watches a folder, processes the files that gets dumped into it, then after processing, it moves the files to another folder.

So i copy a number of files to this folder, and every now and then, i get an exception stating that i cannot gain access to this file, then it goes through the loop again, until it can process it. But that is not the issue, I get the exception but the service remains alive, and the it does what it is supposed to do with the files...it works perfectly.

The problem: When I install this service on a other machine, it processes say...10 files perfectly, but not more, when it gets the exception, the service dies.

Both machine are using .net framework 4 and both have the necessary files. Can anyone point me in the right direction as to what the problem can be?

I have tried sleep and all these things...but I dont think that it's really needed if my machine can do this task effortlessly.

try
            {
                if (System.IO.File.Exists(e.FullPath))
                {

                   // System.Threading.Thread.Sleep(100);
                    System.IO.StreamReader inputfile = new System.IO.StreamReader(e.FullPath);
                    do
                    {
                        // System.Threading.Thread.Sleep(5000);
                        // FileName = e.FullPath.ToString().Remove(0, 24);
                        Ticket = e.Name.ToString(); //FileName.Remove(8, 4);

                        Record = inputfile.ReadLine();
                        if (Record == null)
                            break;
                        if (Record.Contains("ATMID") && i == 0) 
                        {
                            ATMID = Record.Remove(0, 8);
                            i++;
                        }
                        if (Record.Contains("CODE") && i == 1)
                        {
                            StatusCode = Record.Remove(0, 7);
                            i++;
                        }
                        if (Record.Contains("DESCRIPTION") && i == 2)
                        {
                            EventDescription = Record.Remove(0, 14);
                            i++;
                        }
                        if (Record.Contains("ACTION") && i == 3)
                        {
                            ActionCode = Record.Remove(0, 14);
                        }
                        if (Record.Contains("START"))
                        {
                            temp = Record.Remove(0, 7);
                            TimeofEvent = temp.Remove(0, 11);
                            DateofEvent = temp.Substring(1, 10);
                            DateofEvent = DateofEvent.Replace("/", "");
                        }

                        //time.Interval = 60000;
                        //time.Enabled = true;
                     //   System.Threading.Thread.Sleep(100);
                    } while (Record != null);
                    inputfile.Close();
                    SNMPMessageSend(ATMID, StatusCode, EventDescription, ActionCode, TimeofEvent, DateofEvent, Ticket, e);
                }
            }
            catch (System.Exception ex)
            {
                File.Create(@"C:\Work Tasks\exception.txt").Close();
                File.WriteAllText(@"C:\Work Tasks\exception.txt", ex.Message);
            }

Your try/catch block is outside the loop so the loop won't continue.

As to why it's working on your machine I don't know, the setup must be different. Check the service recovery modes on the machine you've installed it to. Most will shut the service down after a number of failures.

The loop does continue when on my machine.

1) There is a loop outside the try/catch you haven't shown
2) It's not an exception
3) You are mistaken about the execution

Your try catch is outside your loop. If an exception gets thrown, your loop will terminate. It is not possible for the loop to continue executing.

ok this is what i did, i removed my try catch, but it did not work as efficiently as opposed to having it there. I added another try inside of my inner loop.

Uncommenting the outer try, works 80% better.

private static void FSWatcherTest_Changed(object source, System.IO.FileSystemEventArgs e)
        {
            string ATMID = "";
            string StatusCode = "";
            string EventDescription = "";
            string DateofEvent = "";
            string TimeofEvent = "";
            string Record = "";
            string ActionCode = "";
            string temp = "";
            string Ticket = "";
            int i = 0;

          //      try
           //     {
                    if (System.IO.File.Exists(e.FullPath))
                    {
                        System.IO.StreamReader inputfile = new System.IO.StreamReader(e.FullPath);
                        do
                        {
                            try
                            {
                               // System.Threading.Thread.Sleep(5000);
                                Ticket = e.Name.ToString();
                                Record = inputfile.ReadLine();
                                if (Record == null)
                                    break;
                                if (Record.Contains("ATMID") && i == 0)
                                {
                                    ATMID = Record.Remove(0, 8);
                                    i++;
                                }
                                if (Record.Contains("CODE") && i == 1)
                                {
                                    StatusCode = Record.Remove(0, 7);
                                    i++;
                                }
                                if (Record.Contains("DESCRIPTION") && i == 2)
                                {
                                    EventDescription = Record.Remove(0, 14);
                                    i++;
                                }
                                if (Record.Contains("ACTION") && i == 3)
                                {
                                    ActionCode = Record.Remove(0, 14);
                                }
                                if (Record.Contains("START"))
                                {
                                    temp = Record.Remove(0, 7);
                                    TimeofEvent = temp.Remove(0, 11);
                                    DateofEvent = temp.Substring(1, 10);
                                    DateofEvent = DateofEvent.Replace("/", "");
                                }
                            }

                            catch (System.Exception exi)
                            {
                                File.WriteAllText(@"C:\Gasper\innerexception.txt", exi.Message);
                            }

                            if (!DateofEvent.Equals("")) 
                            {
                                Record = null;
                                inputfile.Close();
                                SNMPMessageSend(ATMID, StatusCode, EventDescription, ActionCode, TimeofEvent, DateofEvent, Ticket, e);
                            }
                        } while (Record != null);
                        inputfile.Close();
                        //SNMPMessageSend(ATMID, StatusCode, EventDescription, ActionCode, TimeofEvent, DateofEvent, Ticket, e);
                    }
                }
          //      catch (System.Exception ex)
         //       {
          //         File.WriteAllText(@"C:\Gasper\exception.txt", ex.Message);
          //      }          
         //   }

What conditions are you measuring performance by? It works 80% "better"? Howso?

If you use the try/catch outside the loop your loop will exit. There's nothing else to it. It's not magic. Once you exit the loop it won't continue iterating.

Something else is happening. From your method defition it looks like you're hooking the event directly. This means every file change will be picked up by your event.

Based on the conditions of your watcher, this means that every change you make in that folder will fire the event including the changes you make inside the event code.

Everytime the event fires, it will be launching a new thread. I think this is why you believe the loop is continuing to execute. It isn't, it's just that another thread is also executing the loop at the same time.

I get what you saying now...hmmm. I made the following changes.

        private static void FSWatcherTest_Changed(object source, System.IO.FileSystemEventArgs e)
        {
            string ATMID = "";
            string StatusCode = "";
            string EventDescription = "";
            string DateofEvent = "";
            string TimeofEvent = "";
            string Record = "";
            string ActionCode = "";
            string temp = "";
            string Ticket = "";
            int i = 0;


            //try
           // {
               // if (System.IO.File.Exists(e.FullPath))
               // {
                    //  System.IO.StreamReader inputfile = new System.IO.StreamReader(e.FullPath);

                   // using (var inputfile = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                   //    {
                   // using (var inputfile = File.OpenText(e.FullPath))
                   // {
            do
            {
                try
                {
                    //using (var inputfile = File.OpenText(e.FullPath))
                   // {
                        System.IO.StreamReader inputfile = new System.IO.StreamReader(e.FullPath);
                        if (System.IO.File.Exists(e.FullPath))
                        {
                            try
                            {
                                // System.Threading.Thread.Sleep(5000);
                                Ticket = e.Name.ToString();
                                Record = inputfile.ReadLine();
                                if (Record == null)
                                    break;
                                if (Record.Contains("ATMID") && i == 0)
                                {
                                    ATMID = Record.Remove(0, 8);
                                    i++;
                                }
                                if (Record.Contains("CODE") && i == 1)
                                {
                                    StatusCode = Record.Remove(0, 7);
                                    i++;
                                }
                                if (Record.Contains("DESCRIPTION") && i == 2)
                                {
                                    EventDescription = Record.Remove(0, 14);
                                    i++;
                                }
                                if (Record.Contains("ACTION") && i == 3)
                                {
                                    ActionCode = Record.Remove(0, 14);
                                }
                                if (Record.Contains("START"))
                                {
                                    temp = Record.Remove(0, 7);
                                    TimeofEvent = temp.Remove(0, 11);
                                    DateofEvent = temp.Substring(1, 10);
                                    DateofEvent = DateofEvent.Replace("/", "");
                                }
                                if (!DateofEvent.Equals(""))
                                {
                                    Record = null;
                                    inputfile.Close();
                                    SNMPMessageSend(ATMID, StatusCode, EventDescription, ActionCode, TimeofEvent, DateofEvent, Ticket, e);
                                }
                            }
                            catch (System.Exception ex)
                            {
                                File.AppendAllText(@"C:\Gasper\exception.txt", ex.Message);
                            }

                            }

                         //   }

                     }
                    catch (System.Exception exi)
                    {
                        File.WriteAllText(@"C:\Gasper\innerexception.txt", exi.Message);
                    }
                   } while (Record != null);
                           // inputfile.Close();
          }

and now this is not moving onto the next file after the previous has been moved. its like its stuck on what was given in the first place.

 try
                {
                    using (System.IO.StreamReader inputfile = new System.IO.StreamReader(e.FullPath))
                    {
                    if (System.IO.File.Exists(e.FullPath))
                    {

Which events are you triggering on?

You will need Changed, Created and Renamed all pointing to the same method.

Also, the FileSystemWatcher has a habbit or missing events, especially if the event buffer is full. Personally, I don't like using it as it isn't very reliable.

I'm heading into a meeting now but I'll go over some more things when I get back.

Ok, the reason that it's only doing the same file as the original is that the filewatcher won't give you a directory list, only the name of the file that it caught that changed.

So there's not really any point in having a loop in there as you can only ever iterate over the same file.

I guess you could watch for the event and then look up every file in the directory and process those, but you may not get every file, especially if the event comes in early.

Also, you really should exit from the event as quickly as possible (recommended by Microsoft), so consider making the call async.

Your best bet; watch the event, when you enter, grab every file in the directory and process. When you finish, grab another list of files in the directory and process if you have any, otherwise; terminate. If you've managed to iterate an entire directory without a new file appearing, chances are your copy process is finished, otherwise, it will probably be called again when the files have finished copying.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.