// Thread code for the producer 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 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); }
public Location ReadLocationsFromFileData(ConfigRecord configRecord) { // Open the file to read from on the local file system. // If this file is missing then return immediately from this method. if (!File.Exists(configRecord.Filename)) { // Cannot open the file as it does not exist for whatever reason, so return immediately. return(null); } // Open file and load into memory as XML XDocument xmlDoc = XDocument.Load(configRecord.Filename); // Create constituency (should only be one in file but retrieve first to be sure) var con = (from c in xmlDoc.Descendants("Location") select c).First(); Location constituency = new Location(con.Attribute("name").Value); // Obtain candidates from this constituency var cand = from c in con.Descendants("Reading") select c; foreach (var c in cand) { var temperature = c.Element("temperature").Value; var humidity = c.Element("humidity").Value; var date = c.Attribute("date").Value; var value = Int32.Parse(c.Element("value").Value); Particulates candidate = new Particulates(temperature, humidity, date, value); constituency.Particulates.Add(candidate); } return(constituency); }
private ILocationFileReader IOhandler; // Data used when this work is processed - config record public Work(ConfigRecord data, ILocationFileReader IOhandler) //extra param for IcyclistIO { this.configRecord = data; // Data is initialised when the work is instantiated this.IOhandler = IOhandler; }