Exemplo n.º 1
0
        public static void ReportThreadResults(ThreadResult threadResult)
        {
            if (VerboseMode)
            {
                Console.WriteLine(String.Format("{0}: {1} Reporting a score from thread.", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff"), threadResult.ThreadId));
            }
            bool saved = false;

            while (!saved)
            {
                if (VerboseMode)
                {
                    Console.WriteLine(String.Format("{0}: {1} Waiting for lock to write scores.", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff"), threadResult.ThreadId));
                }
                lock (_scores)
                {
                    _executionTime += threadResult.ExecutionTime;
                    foreach (KeyValuePair <String, List <double> > score in threadResult.Scores)
                    {
                        if (VerboseMode)
                        {
                            Console.WriteLine(String.Format("{0}: {4} Reporting a score from {1}, score: {2} (in {3} ms).", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff"), score.Key, score.Value[0], score.Value[1], threadResult.ThreadId));
                        }
                        _scores.Add(score.Key, score.Value);
                    }
                    saved = true;
                }
            }
            _finishedThreads += 1;
        }
        // Beta is the method that will be called when the work item is
        // serviced on the thread pool.
        // That means this method will be called when the thread pool has
        // an available thread for the work item.
        public void Run(object filesObj)
        {
            Guid          uid   = Guid.NewGuid();
            List <String> files = (List <String>)filesObj;

            DateTime start = DateTime.UtcNow;

            ThreadResult threadResult = new ThreadResult();

            threadResult.ThreadId = uid.ToString();

            if (VerboseMode)
            {
                Console.WriteLine(String.Format("{0}: {1} files to process: {2}", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff"), uid.ToString(), files.Count));
            }

            foreach (String file in files)
            {
                try
                {
                    FileInfo fi = new FileInfo(file);
                    if (VerboseMode)
                    {
                        Console.WriteLine(String.Format("{0}: {1} processing: {2}",
                                                        DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff"), uid.ToString(), fi.Name));
                    }

                    Lookup        lookup  = new Lookup(file);
                    List <double> results = new List <double>();
                    DateTime      idTime  = DateTime.UtcNow;
                    int           score   = lookup.Identify(Text);
                    results.Add(score);
                    results.Add((DateTime.UtcNow - idTime).TotalMilliseconds);
                    threadResult.Scores.Add(fi.Name, results);
                }
                catch (Exception e)
                {
                    if (VerboseMode)
                    {
                        Console.WriteLine(String.Format("{0}: Thread {1} experienced an exception procdessing {2}. {3}", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff"), uid.ToString(), file, e.ToString()));
                    }
                }
            }
            threadResult.ExecutionTime = (DateTime.UtcNow - start).TotalMilliseconds;

            if (VerboseMode)
            {
                Console.WriteLine(String.Format("{0}: {1} completed: {2}ms", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff"), uid.ToString(), threadResult.ExecutionTime));
            }

            Callback(threadResult);

            PoolMonitor.Set();
        }