EnqueueTask() public method

public EnqueueTask ( task ) : void
return void
コード例 #1
0
ファイル: FileCopyService.cs プロジェクト: shineRR/ThreadPool
        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);
        }
コード例 #2
0
        public static void WaitAll(List <TaskDelegate> tasks)
        {
            TaskQueue taskQueue = new TaskQueue(10);

            tasks.ForEach(task => { taskQueue.EnqueueTask(task); });
            taskQueue.WaitAll();
        }
コード例 #3
0
ファイル: CopyService.cs プロジェクト: raman-haiduchyk/SPP
 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}");
 }
コード例 #4
0
ファイル: CustomParallel.cs プロジェクト: raman-haiduchyk/SPP
 public static void WaitAll(TaskDelegate[] tasks)
 {
     foreach (var task in tasks)
     {
         _taskQueue.EnqueueTask(task);
     }
     _taskQueue.StopWorking();
 }
コード例 #5
0
        static void Main(string[] args)
        {
            TaskQueue taskQueue = new TaskQueue(5);

            for (int i = 0; i < 20; i++)
            {
                taskQueue.EnqueueTask(Taskmethod);
            }
            taskQueue.Wait();
        }
コード例 #6
0
        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();
        }
コード例 #7
0
        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);
            }
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: che6urek/MPP-labs
        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);
            }
        }
コード例 #9
0
            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();
            }
コード例 #10
0
ファイル: Program.cs プロジェクト: EvdAnton/1-6Labs-SPP-BSUIR
        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));
            }
        }
コード例 #11
0
        //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");
        }
コード例 #12
0
 public void EncueueTask(File file)
 => taskQueue.EnqueueTask(file);
コード例 #13
0
        public void EnqueueTask_Exc()
        {
            TaskQueue q = new TaskQueue();

            q.EnqueueTask(null, null);
        }