예제 #1
0
        static void Example_003(FindYourGuid findYourGuid, CancellationToken ct)
        {
            //somente para contar o tempo gasto entre todas as tasks;
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            List <Task> tasks = new List <Task>();

            for (int i = 0; i < 10; i++)
            {
                var newTask = Task.Factory.StartNew(() =>
                {
                    string result = service.ToDo(model, ct);
                    Console.WriteLine(result);
                }, ct);

                tasks.Add(newTask);
            }

            Task.WaitAll(tasks.ToArray());

            stopWatch.Stop();

            Console.WriteLine($"\n\n\nAll Tasks is finished! Elapsed Time: {stopWatch.Elapsed}\n\n");
        }
예제 #2
0
        static void Example_001(FindYourGuid findYourGuid)
        {
            string result = service.ToDo(findYourGuid);

            Console.WriteLine(result);

            Console.ReadKey();
        }
예제 #3
0
        static async void Example_002(FindYourGuid findYourGuid, CancellationToken ct)
        {
            string result = await Task.Factory.StartNew(() =>
            {
                return(service.ToDo(model, ct));
            }, ct);

            Console.WriteLine(result);
        }
예제 #4
0
        static void Main(string[] args)
        {
            service = new Working();
            _cts    = new CancellationTokenSource();
            model   = new FindYourGuid('0', 12, 10000);


            //Example_001(model);
            //Example_002(model, _cts.Token);
            //Example_003(model, _cts.Token);
            Example_004(model, _cts.Token);


            Console.WriteLine("\n\n The Main is finished...");
            Console.ReadKey();
        }
예제 #5
0
        /// <summary>
        /// Worker
        /// </summary>
        /// <param name="findYourGuid"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        public string ToDo(FindYourGuid findYourGuid, CancellationToken ct)
        {
            //measures time elapsed
            Stopwatch stopwatch = new Stopwatch();

            //start counting
            stopwatch.Start();

            int countLoop = 0;

            //output
            string result = string.Empty;

            do
            {
                string _guid = Guid.NewGuid().ToString();

                countLoop++;

                if (_guid.Count(c => c == findYourGuid.CharToFind) >= findYourGuid.HowManyTimes)
                {
                    var charArray = _guid.Select(c =>
                    {
                        char newChar = c != findYourGuid.CharToFind && c != '-' ? 'x' : c; return(newChar);
                    });

                    string _guidChar = string.Concat(charArray);

                    result = $"\nSuccessful!\nThere were {countLoop} repetitions to find the Guid.\nOrigin: {_guid}\nSpotlight: {_guidChar}\nElapsed time: {stopwatch.Elapsed}\n";
                }

                if (ct.IsCancellationRequested)
                {
                    result = $"\nWork was canceled after {stopwatch.Elapsed}. {countLoop} repetitions.\n";
                }

                if (stopwatch.ElapsedMilliseconds >= findYourGuid.LimitTimeInMilliseconds)
                {
                    result = $"the time of {stopwatch.Elapsed} seconds is over. {countLoop} repetitions.";
                }
            }while (string.IsNullOrEmpty(result));

            //stop counting
            stopwatch.Stop();

            return(result);
        }
예제 #6
0
        static void Example_004(FindYourGuid findYourGuid, CancellationToken ct)
        {
            List <Task> tasks = new List <Task>();

            for (int i = 0; i < 10; i++)
            {
                var newTask = Task.Factory.StartNew(() =>
                {
                    string result = service.ToDo(model, ct);
                    Console.WriteLine(result);
                }, ct);

                tasks.Add(newTask);
            }

            //Iniciar todas as tarefas e seguir a diante!
            Task.WhenAll(tasks.ToArray());

            //cancelar após 9 segundos;
            _cts.CancelAfter(9000);
        }
예제 #7
0
 /// <summary>
 /// Worker
 /// </summary>
 /// <param name="findYourGuid"></param>
 /// <returns></returns>
 public string ToDo(FindYourGuid findYourGuid)
 {
     return(ToDo(findYourGuid, CancellationToken.None));
 }