public static List <VRASCopyBlob> GetFiles(VRASCopyConfiguration config) { try { List <VRASCopyBlob> blobList = new List <VRASCopyBlob>(); DirectoryInfo dir = new DirectoryInfo(config.BaseFolderPath); IEnumerable <FileInfo> files; IEnumerable <FileInfo> qry; // set the query of folder enum based on the CopyType if (config.CopyType == SourceTypeCopyType.CombineToRoot || config.CopyType == SourceTypeCopyType.Mirror) { files = dir.EnumerateFiles("*", SearchOption.AllDirectories); } else { files = dir.EnumerateFiles("*", SearchOption.TopDirectoryOnly); } // choose query on file list returned based on the flag for copy 0 byte if (config.CopyZeroByteFiles) { qry = from file in files where file.LastWriteTimeUtc < DateTime.UtcNow.AddSeconds(-1 * config.NewFileDelaySeconds) && file.Length >= 0 orderby file.LastWriteTimeUtc ascending select file; } else { qry = from file in files where file.LastWriteTimeUtc < DateTime.UtcNow.AddSeconds(-1 * config.NewFileDelaySeconds) && file.Length > 0 orderby file.LastWriteTimeUtc ascending select file; } foreach (FileInfo f in qry) { if (StringMatchesFileAndFolderRegEx(f, config.FileRegExFilter, config.FolderRegExFilter)) { VRASCopyBlob blob = new VRASCopyBlob(config, f); blobList.Add(blob); } else { } } return(blobList); } catch (Exception ex) { throw new Exception(String.Format("Error Enumerating Directory: {0}", ex.Message)); } }
// method the thread uses to find new files to copy private void RunJobFinder() { int timesRun = 0; while (!this.timeToQuit) { lock (this.queue) { if (this.queue.Count <= 0 && this.workingCopyThreads == 0) { try { List <VRASCopyBlob> blobList = VRASCopyBlob.GetFiles(this.copyConfig); foreach (VRASCopyBlob copyJob in blobList) { // need to make the workitems for the blob to know what to copy copyJob.PopulateCopyWork(); this.QueueUserWorkItem(new WaitCallback(copyJob.DoWork), new object()); } } catch (Exception ex) { VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, "Error while getting files: " + ex.Message + "...Worker Thread to find files is going to sleep for its scheduled duration.", System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.FileCopyError), VRASLogEvent.EventSourceDefault); } } } // if it is a service increase the run count if (this.timesToRunTheWorkFinder != -1) { timesRun++; } else { // sleep if it is a service (-1) times to run this.workWaiting.WaitOne(this.copyConfig.SleepBetweenBatchesMs); } // don't sleep if there are no more runs to do if ((this.timesToRunTheWorkFinder - timesRun) > 0) { this.workWaiting.WaitOne(this.copyConfig.SleepBetweenBatchesMs); } // see if it ran enough times -1 means forever. Then we can set the flag to quit to true. If we don't wait then the copy threads will all want to quit now. if (this.timesToRunTheWorkFinder != -1 && timesRun >= this.timesToRunTheWorkFinder) { // wait for the queue to drain bool stillWaiting = true; int count; while (stillWaiting) { lock (this.queue) { count = this.queue.Count; } if (count > 0) { Thread.Sleep(1000); } else { stillWaiting = false; } } this.timeToQuit = true; } } }