Ejemplo n.º 1
0
 /// <summary>
 /// `crosshair`: Create a crosshair that follows the target for a limited amount of time
 /// and saves the target's position in public data hoisting.
 /// </summary>
 public static TaskPattern Crosshair(string style, GCXF <Vector2> locator, GCXF <float> homeSec, GCXF <float> stickSec,
                                     ReflectEx.Hoist <Vector2> locSave, ExBPY indexer)
 {
     var cindexer = GCXF(indexer);
     var saver    = Async("_$CROSSHAIR_INVALID", _ => V2RV2.Zero, AsyncPatterns.GCRepeat2(
                              x => 1,
                              x => ETime.ENGINEFPS * homeSec(x),
                              GCXFRepo.RV2Zero,
                              new[] { GenCtxProperty.SaveV2((locSave, cindexer, locator)) }, new[] { AtomicPatterns.Noop() }
Ejemplo n.º 2
0
        public void ParallelForEachWithConsumingEnumerableTest()
        {
            // All the MoveNext calls should be made on the same thread
            var enumerable = new MyEnumerable(20);

            AsyncPatterns.ForEachWithEnumerationOnMainThread <int>(enumerable, (i) =>
            {
                Debug.WriteLine("Loop: {0}", i);
            });
        }
Ejemplo n.º 3
0
        public async Task NeedOnlyOneTest()
        {
            string result = await AsyncPatterns.NeedOnlyOne <string>(
                ct => ExecuteWebRequest("https://www.google.com", ct),
                ct => ExecuteWebRequest("https://www.bing.com", ct),
                ct => ExecuteWebRequest("https://www.yahoo.com", ct)
                );

            Debug.WriteLine("Result: {0}", result);
        }
Ejemplo n.º 4
0
        public async Task WhenWhenAllOrFirstExceptionTest()
        {
            var tasksInput = new List <Task <string> >();

            tasksInput.Add(Task.Factory.StartNew(() =>
            {
                Task.Delay(10000).Wait();
                return("LongRunning");
            }, TaskCreationOptions.LongRunning));
            tasksInput.Add(Task.Factory.StartNew(() =>
            {
                Task.Delay(5000).Wait();
                return("ShortRunning");
            }));
            tasksInput.Add(Task.Factory.StartNew(() =>
            {
                Task.Delay(1000).Wait();
                int i = 1;
                if (i == (5 / 4))
                {
                    throw new ArgumentNullException("Thrown");
                }

                return("Could Be Exceptional");
            }));

            var taskResult = AsyncPatterns.WhenAllOrFirstException(tasksInput.AsEnumerable());

            await taskResult;

            Debug.WriteLine("IsFaulted: {0}, IsCompleted: {1}, IsCancelled: {2}",
                            taskResult.IsFaulted, taskResult.IsCompleted, taskResult.IsCanceled);
            if (taskResult.IsCanceled)
            {
                Debug.WriteLine("Exception: {0}", taskResult.Exception);
            }
            else
            {
                foreach (var result in taskResult.Result)
                {
                    Debug.WriteLine("Result: " + result);
                }
            }
        }
Ejemplo n.º 5
0
        public async Task TestInterleaved()
        {
            int counter = 0;

            // Create some sample tasks
            IEnumerable <Task <int> > tasks = (from _ in Enumerable.Range(0, 100)
                                               select Task.Factory.StartNew <int>(() =>
            {
                Task.Delay(50 + 10 * counter).Wait();
                counter++;
                return(counter);
            })).ToList();

            var interleavedTasks = AsyncPatterns.InterleaveTasks(tasks);

            foreach (var task in interleavedTasks)
            {
                // Access each task as they complete
                int result = await task;
                Debug.WriteLine("Task Id: {0}", result);
            }
        }
Ejemplo n.º 6
0
        public void ProducerConsumerWithBlockingCollectionTest()
        {
            string inputFilePath = @"D:\\tempInput.txt", outputFilePath = @"D:\\tempOutput.txt";

            try
            {
                File.Delete(inputFilePath);
                File.Delete(outputFilePath);
            }
            catch (Exception)
            { }

            for (int i = 1; i < 100000000; i *= 10)
            {
                File.AppendAllLines(inputFilePath, new string[] { "Merhaba + " + i + " nasilsin" });
            }

            AsyncPatterns.ProcessFile(inputFilePath, outputFilePath, (line) =>
            {
                Debug.WriteLine("{0}-{1}", Thread.CurrentThread.ManagedThreadId, line);
                Thread.Sleep(10 + 1000 / (line.Length - 18));
                return(Regex.Replace(line, @"\s+", ", "));
            });
        }
Ejemplo n.º 7
0
        public async Task RetryOnFaultTest()
        {
            int    i      = 0;
            string result = await AsyncPatterns.RetryOnFault <string>(
                () => Task.Factory.StartNew <string>(() =>
            {
                i++;
                Debug.WriteLine("Retry Value: {0}", i);
                Task.Delay(1000).Wait();
                if (i < 3)
                {
                    throw new ArgumentException("Exceptions");
                }
                return(string.Empty);
            }),
                5,
                () => {
                Debug.WriteLine("Retrying ");
                return(Task.Delay(1000));
            }
                );

            Debug.WriteLine("Done");
        }