Ejemplo n.º 1
0
        private void BeginSearch()
        {
            if (TermPool == null || TermPool.Count < 1)
            {
                MessageBox.Show("Term cannot be empty.", "Input missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrWhiteSpace(OperatorPool))
            {
                MessageBox.Show("You must select at least one operation.", "Input missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            equationArgs = new EquationFinderArgs(targetValue, numberOfOperations, TermPool, OperatorPool);

            string[] previousResults = GetOutputLines();
            if (previousResults != null && previousResults.Length > 0)
            {
                threadArgs = new ThreadSpawnerArgs(previousResults.ToList(), null, timeToLive, numberOfThreads, numberOfRounds, equationArgs);
            }
            else
            {
                threadArgs = new ThreadSpawnerArgs(null, timeToLive, numberOfThreads, numberOfRounds, equationArgs);
            }

            if (backgroundWorker_ThreadSpawner.IsBusy == false)
            {
                ToggleControlsVisibility(false);
                timerCollectResults.Start();
                backgroundWorker_ThreadSpawner.RunWorkerAsync(threadArgs);
            }
        }
Ejemplo n.º 2
0
        public void Find()
        {
            if (string.IsNullOrWhiteSpace(Settings.Term_Pool))
            {
                throw new Exception("Setting TermPool is empty. Check the configuration file, and set the AppSetting value to a comma delimited list of allowed values for the key \"Term.Pool\".");
            }

            int        parseOut = 0;
            List <int> termPool = new List <int>();

            foreach (string term in Settings.Term_Pool.Split(','))
            {
                parseOut = 0;
                if (int.TryParse(term, out parseOut))
                {
                    termPool.Add(parseOut);
                }
            }

            equationArgs = new EquationFinderArgs(Settings.Equations_Goal, ResultPredicate.IsEqualTo, Settings.Operations_Quantity, termPool, Settings.Operand_Pool);
            threadArgs   = new ThreadSpawnerArgs(previousfoundResults, LogSolution, Settings.Round_TimeToLive, Settings.Round_Threads, Settings.Round_Quantity, equationArgs);

            ThreadedEquationFinder <AlgebraicTuple> equationFinder = new ThreadedEquationFinder <AlgebraicTuple>(threadArgs);

            if (File.Exists(outputFilename))
            {
                equationFinder.Results.AddRange(File.ReadAllLines(outputFilename));
            }
            equationFinder.Run();
        }
        public List <string> ThreadSpawner(ThreadSpawnerArgs threadArgs, EquationThreadManagerDelegate equationManager)
        {
            List <string> strResults = new List <string>();

            try
            {
                List <EquationThreadManagerDelegate> threadDelegateList = new List <EquationThreadManagerDelegate>();
                List <IAsyncResult>    threadHandletList = new List <IAsyncResult>();
                List <EquationResults> threadResultList  = new List <EquationResults>();

                // Make list of delegates that will become threads
                threadDelegateList.AddRange(
                    Enumerable.Repeat(equationManager, threadArgs.NumberOfThreads)
                    );

                // Invoke all the threads
                foreach (EquationThreadManagerDelegate thread in threadDelegateList)
                {
                    threadHandletList.Add(thread.BeginInvoke(threadState, null /*new AsyncCallback(OnThreadCompleted)*/, null));
                }

                // Await the result of each thread
                int counter = 0;
                foreach (var thread in threadDelegateList)
                {
                    threadResultList.AddRange(thread.EndInvoke(threadHandletList[counter]));
                    counter++;
                }

                // Free up some resources
                threadHandletList.RemoveAll(d => true);
                threadDelegateList.RemoveAll(d => true);
                threadHandletList  = null;
                threadDelegateList = null;


                // Format the results as a List of strings
                foreach (EquationResults item in threadResultList)
                {
                    if (item.IsSolution)
                    {
                        strResults.Add(item.EquationText);
                    }
                    else
                    {
                        strResults.Add(ExpirationMessage);
                    }
                }

                // Free up some resources
                threadResultList.RemoveAll(r => true);
                threadResultList = null;
            }
            finally
            {
                if (strResults == null)
                {
                    strResults = new List <string>();
                }
            }

            return(strResults);
        }
 public ThreadedEquationFinder(ThreadSpawnerArgs spawnerArgs)
 {
     Results                 = new List <string>();
     threadSpawnerArgs       = spawnerArgs;
     TotalEquationsGenerated = 0;
 }
Ejemplo n.º 5
0
 public ThreadState(ThreadSpawnerArgs threadArgs, DateTime timeToStop)
 {
     this.ThreadArgs  = threadArgs;
     this.TimeToStop  = new DateTime(timeToStop.Ticks);
     this.CancelToken = new CancellationTokenSource();
 }