public void StartCopying(string src, string dest) { CreateMissingDirs(src, dest); List <String> dirFileList = GetFiles(src); FileAttributes attr = File.GetAttributes(dest); if (!attr.HasFlag(FileAttributes.Directory)) { return; } Directory.CreateDirectory(dest); foreach (var file in dirFileList) { string fileName = file.Replace(src, dest); _taskQueue.EnqueueTask(delegate { try { File.Copy(file, fileName, true); Interlocked.Increment(ref _copiedFiles); Console.WriteLine("from: " + file + ", to: " + fileName + " by " + Thread.CurrentThread.Name); } catch (Exception e) { Console.WriteLine(e); } }); } _taskQueue.Dispose(); PrintCopiedFiles(ref _taskQueue.ThreadQueue); }
public static void WaitAll(List <TaskDelegate> tasks) { TaskQueue taskQueue = new TaskQueue(10); tasks.ForEach(task => { taskQueue.EnqueueTask(task); }); taskQueue.WaitAll(); }
public void StartCopy() { foreach (var dir in Directory.GetDirectories(_src, "*", SearchOption.AllDirectories)) { string newDir = dir.Replace(_src, _dest); try { Directory.CreateDirectory(newDir); } catch (Exception ex) { Console.WriteLine($"Creating directory error ({ex.Message})."); } } foreach (var file in Directory.GetFiles(_src, "*.*", SearchOption.AllDirectories)) { string newFile = file.Replace(_src, _dest); _taskQueue.EnqueueTask(delegate { try { File.Copy(file, newFile, true); Console.WriteLine($"File copied:\n from {file}\n to {newFile}"); Interlocked.Increment(ref _filesCopied); } catch (Exception ex) { Console.WriteLine($"Copying file error ({ex.Message}):\n from {file}\n to {newFile}"); Interlocked.Increment(ref _copyErrorsOccured); } }); } _taskQueue.StopWorking(); Console.WriteLine($"Files copied: {_filesCopied}\nErrors occured: {_copyErrorsOccured}"); }
public static void WaitAll(TaskDelegate[] tasks) { foreach (var task in tasks) { _taskQueue.EnqueueTask(task); } _taskQueue.StopWorking(); }
static void Main(string[] args) { TaskQueue taskQueue = new TaskQueue(5); for (int i = 0; i < 20; i++) { taskQueue.EnqueueTask(Taskmethod); } taskQueue.Wait(); }
static void Main(string[] args) { Logger logger = Logger.getInstance(); var taskQueue = new TaskQueue(6); for (int i = 0; i < 20; i++) { taskQueue.EnqueueTask(TestTask, i % 5); logger.LogInfo("Поставлена задача номер: " + i); } Console.ReadLine(); taskQueue.Close(); }
private void FindFiles(DirectoryInfo directory) { var files = directory.GetFiles(); count += files?.Length ?? 0; foreach (var file in files) { FilesQueue.EnqueueTask(file.FullName); } foreach (var dir in directory.GetDirectories()) { FindFiles(dir); } }
private static void CopyDirectory(string directoryFrom, string directoryTo) { Directory.CreateDirectory(directoryTo); foreach (var file in Directory.GetFiles(directoryFrom)) { var newFile = file.Replace(directoryFrom, directoryTo); _threadPool.EnqueueTask(() => CopyFile(file, newFile)); } foreach (var dir in Directory.GetDirectories(directoryFrom)) { var newDir = dir.Replace(directoryFrom, directoryTo); CopyDirectory(dir, newDir); } }
public static void WaitAll(TaskDelegate[] delegates) { var taskQueue = new TaskQueue(); var delegatesCount = delegates.Length; var counter = 0; for (var i = 0; i < delegates.Length; i++) { delegates[i] = delegates[i] + (() => Interlocked.Increment(ref counter)); taskQueue.EnqueueTask(delegates[i]); } while (counter != delegatesCount) { Thread.Yield(); } taskQueue.Dispose(); }
static void CopyDirectory(DirectoryInfo source, DirectoryInfo destination) { FileInfo[] files = source.GetFiles(); foreach (FileInfo file in files) { var state = new List <string> { destination.FullName, file.FullName }; taskQueue.EnqueueTask(state, CopyFile); } DirectoryInfo[] dirs = source.GetDirectories(); foreach (DirectoryInfo dir in dirs) { string destinationDir = Path.Combine(destination.FullName, dir.Name); CopyDirectory(dir, new DirectoryInfo(destinationDir)); } }
//private static Logger _log = LogManager.GetCurrentClassLogger(); public static void WaitAll(TaskDelegate[] taskDelegates) { //дожидается, пока выполнятся все делегаты int numberOfTasks = taskDelegates.Length; int amountDone = 0; ManualResetEvent signal = new ManualResetEvent(false); //несигнальное состояние foreach (var func in taskDelegates) { amountDone++; _taskQueue.EnqueueTask(() => func()); log.Add("Executing task " + amountDone); //_log.Info("Executing task " + amountDone); TaskExecuted(ref numberOfTasks, signal); } //блокировка потоков и выполнение их до завешения log.Add("Waiting for " + taskDelegates.Length + " tasks"); //_log.Info("Waiting for " + taskDelegates.Length + " tasks"); signal.WaitOne(); log.Add("Tasks executed"); //_log.Info("Tasks executed"); }
public void EncueueTask(File file) => taskQueue.EnqueueTask(file);
public void EnqueueTask_Exc() { TaskQueue q = new TaskQueue(); q.EnqueueTask(null, null); }