Tuesday, May 25, 2010

Using SortedList for FileWatcher

I need to do an update and archive files based on a condition in my application. So i have used sortedList to get all the files by processing the thread in my service. For details check my last post on this Multi Threading Using parameters for reference.

So I have updated on of my method DoTXT to achieve this from my previous post.

/// <summary>
        /// Thread 2, Displays that we are in thread 2
        /// </summary>
        public void DoTXT()
        {
            while (_stopThreads == false)
            {
                lock (this)
                {
                    // Write your TXT LOGIC HERE, Moving files and updating database etc
                    Console.WriteLine("Display Thread 2 " + strExt);
                    _threadOutput = "Hello Thread 2";
                    Thread.Sleep(1000);  // simulate a lot of processing
                    string[] Files = Directory.GetFiles("D://ThreadSample//Files//");
                    SortedList sList = new SortedList();
                    if (Files.Length > 0)
                    {
                        foreach (string s in Files)
                        {
                            try
                            {
                                if (Path.GetExtension(s).ToUpper().Equals(strExt))
                                {
                                    try
                                    {
                                        // if you date falls between two dates you need to change the condition while comparing.
                                        // Check the File class for rest of useful methods you require..File.GetLastAccessTime
                                        if (File.GetLastWriteTime(s).ToShortDateString().Equals(strDateTime))
                                        {
                                            DateTime dt = File.GetLastWriteTime(s);
                                            sList.Add(dt, Path.GetFullPath(s));
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        throw ex;
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                        // Getting information from the SortedList.
                        foreach (string filename in sList.Values)
                        {
                            // Get all these file to zip for archive
                            Console.WriteLine("filename with sorted list." + filename);
                            // Do the db logic..
                        }
                    }
                    // we are in thread #2
                    Console.WriteLine("DoTXT Output --> {0}", _threadOutput + " datetime: -- > " + strDateTime);
                }
            }
        }

No comments:

Post a Comment