예제 #1
0
        public void ThreadWorker_ProofOfConcept()
        {
            var finalOutput = new List <string>();
            var bkg         = new ThreadWorker <int, string>();

            bkg.Start(_DoBkgWork);
            bkg.OutputResults.DataReceived += _OnOutputDataReceived;

            bkg.InputToProcess.Add(5);
            bkg.InputToProcess.Add(1088050);

            bkg.InputToProcess.Add(1010155001);

            bkg.ShutdownGracefullyAndWaitForCompletion().ContinueWith(_ =>
            {
                Assert.IsFalse(bkg.IsActive);
                string result = String.Join(",", finalOutput);
                Assert.AreEqual("5,1,88,5,1,1,155,1", result);
            });

            void _DoBkgWork(ThreadWorkerDataTracker <int, TNone, string> data)
            {
                while (data.ShouldContinue || data.InputToProcess.Any())
                {
                    if (!data.InputToProcess.Any())
                    {
                        _Yield();
                        continue;
                    }

                    // Grab the input state
                    int number = data.InputToProcess.TakeNext();

                    // Process
                    List <string> output = new List <string>();
                    output.AddRange(number.ToString().Split(new[] { '0' }, StringSplitOptions.RemoveEmptyEntries));

                    // Set the output state
                    data.OutputResults.AddRange(output);
                    data.OutputResults.NotifyDataReceived();
                    _Yield();
                }

                void _Yield()
                {
                    Thread.Sleep(0);
                }
            }

            void _OnOutputDataReceived(object sender, EventArgs e)
            {
                finalOutput.AddRange(bkg.OutputResults.TakeAll());
            }
        }
예제 #2
0
        public void ThreadWorker_ExecutionState_Test()
        {
            var finalOutput = new List <string>();
            var bkg         = new ThreadWorker <int, char, string>();

            // Split all incoming numbers on 0 & 5
            bkg.ExecutionState.Add('0');
            bkg.ExecutionState.Add('5');
            bkg.Start(_DoBkgWork);

            bkg.OutputResults.DataReceived += _OnOutputDataReceived;

            bkg.InputToProcess.Add(5);
            bkg.InputToProcess.Add(1088050);
            bkg.ShutdownGracefullyAndWaitForCompletion().ContinueWith(_ =>
            {
                Assert.IsFalse(bkg.IsActive);
                string result = String.Join(",", finalOutput);
                Assert.AreEqual("1,88", result);
            });

            void _DoBkgWork(ThreadWorkerDataTracker <int, char, string> data)
            {
                while (data.ShouldContinue || data.InputToProcess.Any())
                {
                    if (!data.InputToProcess.Any())
                    {
                        Thread.Sleep(0);
                        continue;
                    }

                    // Grab the input state
                    int    number  = data.InputToProcess.TakeNext();
                    char[] splitOn = data.ExecutionState.ToArray();

                    // Process
                    List <string> output = new List <string>();
                    output.AddRange(number.ToString().Split(splitOn, StringSplitOptions.RemoveEmptyEntries));

                    // Set the output state
                    data.OutputResults.AddRange(output);
                    data.OutputResults.NotifyDataReceived();
                    Thread.Sleep(0);
                }
            }

            void _OnOutputDataReceived(object sender, EventArgs e)
            {
                finalOutput.AddRange(bkg.OutputResults.TakeAll());
            }
        }