Exemple #1
0
        /// <summary>
        /// Thread code for the producer
        /// </summary>
        public void run()
        {
            ConfigRecord configRecord = null;

            // While not finished, generate a new work item and enqueue it on the PCQueue, output that this producer has
            // produced a new item (and what it is called)
            while (!Finished)
            {
                // Lock configuration file and obtain next filename to process
                // If there are no filenames left then set filename to null so that nothing is produced
                lock (configFile)
                {
                    if (configFile.NextRecord < configFile.configRecords.Count)
                    {
                        configRecord = configFile.configRecords[configFile.NextRecord++];
                    }
                    else
                    {
                        configRecord = null;
                    }
                }

                // only queue item if there is a config record to read
                if (configRecord != null)
                {
                    // Enqueue a new work item, increment the counter as this work is produced
                    pcQueue.enqueueItem(new Work(configRecord, IOhandler));

                    // Output a message to state that this producer has produced a work item
                    Console.WriteLine("Producer:{0} has created and enqueued Work Item:{1}", id, configRecord.ToString());
                }

                // Simulate producer activity running for duration milliseconds
                Thread.Sleep(duration);
            }

            // Decrement the number of running producer threads
            RunningThreads--;

            // Output that this producer has finished
            Console.WriteLine("Producer:{0} has finished", id);
        }