示例#1
0
        static async Task Main(string[] args)
        {
            //TASK: Discussion! Which of the below will be faster? Why?

            var slowProcessor = new SlowProcessor();
            var timer         = new Stopwatch();

            Console.WriteLine("Hello Lab 1!");

            var countingTarget = 5;

            Console.WriteLine($"I'm going to count to {countingTarget} now, using a SYNCHRONOUS method. Lets see how long that takes...");

            timer.Start();
            var syncCountToANumberSlowly = slowProcessor.CountToANumberSlowly(countingTarget);

            WriteResultsToConsole(timer, syncCountToANumberSlowly);

            Console.WriteLine($"Now I'm going to count to {countingTarget}, this time using an ASYNC method. Lets see how long that takes...");

            timer.Restart();
            var asyncCountToANumberSlowly = await slowProcessor.CountToANumberSlowlyAsync(countingTarget);

            WriteResultsToConsole(timer, asyncCountToANumberSlowly);

            Console.WriteLine("Press the ANY key to close.");
            Console.ReadKey();
        }
示例#2
0
        // Runs on Background worker thread
        void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = (BackgroundWorker)sender;

            int result     = 0;
            int iterations = (int)e.Argument;

            SlowProcessor processor = new SlowProcessor((int)e.Argument);

            foreach (var current in processor)
            {
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                if (worker.WorkerReportsProgress)
                {
                    int    percentageCompletion = (int)((float)current / (float)iterations * 100);
                    string progressMessage      = string.Format("Iteration {0} of {1}", current, iterations);

                    // Send the percentage completion and progress message.
                    worker.ReportProgress(percentageCompletion, progressMessage);
                }

                result = current;
            }

            e.Result = result;
        }
示例#3
0
        static async Task Main(string[] args)
        {
            //TASK: First, implement the commented out code below.

            var slowProcessor = new SlowProcessor();
            var timer         = new Stopwatch();

            Console.WriteLine("Hello Lab 3!");

            var countingTarget1 = 2;
            var countingTarget2 = 7;
            var countingTarget3 = 3;

            Console.WriteLine(
                $"I'm going to count to {countingTarget1}, then I'm going to count to  {countingTarget2}, and then I'm going to going to count to {countingTarget3} using a SYNCHRONOUS method. Lets see how long that takes...");

            timer.Start();
            var syncCountToANumberSlowly1 = slowProcessor.CountToANumberSlowly(countingTarget1);
            var syncCountToANumberSlowly2 = slowProcessor.CountToANumberSlowly(countingTarget2);
            var syncCountToANumberSlowly3 = slowProcessor.CountToANumberSlowly(countingTarget3);

            WriteResultsToConsole(timer,
                                  new[] { syncCountToANumberSlowly1, syncCountToANumberSlowly2, syncCountToANumberSlowly3 });

            Console.WriteLine($"Now I'm going to do that again, this time using an ASYNC method. Lets see how long that takes...");

            timer.Restart();
            var aggregatedTaskResult = await Task.WhenAll(
                slowProcessor.CountToANumberSlowlyAsync(countingTarget1),
                slowProcessor.CountToANumberSlowlyAsync(countingTarget2),
                slowProcessor.CountToANumberSlowlyAsync(countingTarget3));

            var asyncCountToANumberSlowly1 = aggregatedTaskResult.GetValue(0).ToString();
            var asyncCountToANumberSlowly2 = aggregatedTaskResult.GetValue(1).ToString();
            var asyncCountToANumberSlowly3 = aggregatedTaskResult.GetValue(2).ToString();

            WriteResultsToConsole(timer,
                                  new[] { asyncCountToANumberSlowly1, asyncCountToANumberSlowly2, asyncCountToANumberSlowly3 });

            if (timer.Elapsed.TotalSeconds > 8)
            {
                Console.WriteLine("This could be done faster...");
            }
            else
            {
                Console.WriteLine("Yay I'm fast now!");
            }

            Console.WriteLine("Press the ANY key to close.");
            Console.ReadKey();
        }
示例#4
0
        //Runs on the background thread
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            int iterations = (int)e.Argument;
            int result     = 0;

            SlowProcessor processor = new SlowProcessor(iterations);

            foreach (var current in processor)
            {
                result = current;
            }

            e.Result = result;
        }
示例#5
0
        static async Task Main(string[] args)
        {
            //TASK: First, implement the commented out code below.

            var slowProcessor = new SlowProcessor();

            var timer = new Stopwatch();

            Console.WriteLine("Hello Lab 2!");

            var countingTarget1 = 2;
            var countingTarget2 = 7;
            var countingTarget3 = 3;

            Console.WriteLine(
                $"I'm going to count to {countingTarget1}, then I'm going to count to  {countingTarget2}, and then I'm going to going to count to {countingTarget3} using a SYNCHRONOUS method. Lets see how long that takes...");

            timer.Start();
            var syncCountToANumberSlowly1 = slowProcessor.CountToANumberSlowly(countingTarget1);
            var syncCountToANumberSlowly2 = slowProcessor.CountToANumberSlowly(countingTarget2);
            var syncCountToANumberSlowly3 = slowProcessor.CountToANumberSlowly(countingTarget3);

            WriteResultsToConsole(timer,
                                  new[] { syncCountToANumberSlowly1, syncCountToANumberSlowly2, syncCountToANumberSlowly3 });

            Console.WriteLine(
                $"Now I'm going to do that again, this time using an ASYNC method. Lets see how long that takes...");

            timer.Restart();
            //TODO: Perform the same operations as above, using the async method...
            Console.WriteLine("\n\nAaarrrghghhh! Code missing!");

            var asyncCountToANumberSlowly1 = await slowProcessor.CountToANumberSlowlyAsync(countingTarget1);

            var asyncCountToANumberSlowly2 = await slowProcessor.CountToANumberSlowlyAsync(countingTarget2);

            var asyncCountToANumberSlowly3 = await slowProcessor.CountToANumberSlowlyAsync(countingTarget3);

            WriteResultsToConsole(timer,
                                  new[] { asyncCountToANumberSlowly1, asyncCountToANumberSlowly2, asyncCountToANumberSlowly3 });

            Console.WriteLine("Press the ANY key to close.");
            Console.ReadKey();
        }
        public void Init()
        {
            IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory;

            IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-timeout-cache-config5.xml");

            ccf.Config = config;

            m_cache = CacheFactory.GetCache("dist-test");

            m_cache.Clear();

            m_cache[m_key] = 10.0;

            SlowProcessor slowProcessor = new SlowProcessor
            {
                Time        = 10000,
                ReturnValue = m_expectedResult
            };


            m_priorityProcessorDefault = new PriorityProcessor(slowProcessor)
            {
                RequestTimeoutMillis = (long)PriorityTaskTimeout.Default
            };

            m_priorityProcessorNone = new PriorityProcessor(slowProcessor)
            {
                RequestTimeoutMillis = (long)PriorityTaskTimeout.None
            };

            SlowAggregator slowAggregator = new SlowAggregator();

            m_priorityAggregatorDefault = new PriorityAggregator(slowAggregator)
            {
                RequestTimeoutMillis = (long)PriorityTaskTimeout.Default
            };

            m_priorityAggregatorNone = new PriorityAggregator(slowAggregator)
            {
                RequestTimeoutMillis = (long)PriorityTaskTimeout.None
            };
        }