//Generate the preview private void previewButton_Click(object sender, EventArgs e) { //Get the comboboxes. They seem to always be in reverse order. This method may go terribily wrong later. List <ComboBox> comboBoxes = individualModeDataGroupBox.Controls.OfType <ComboBox>().ToList(); comboBoxes.Reverse(); //Get the textboxes. They seem to always be in reverse order. This method may go terribily wrong later. List <TextBox> textBoxes = individualModeDataGroupBox.Controls.OfType <TextBox>().ToList(); textBoxes.Reverse(); Plate p = new Plate(); p.count = (int)deviceCountNumericUpDown.Value; for (int i = 0; i < deviceCountNumericUpDown.Value; ++i) { DeviceTypes d = (DeviceTypes)Enum.Parse(typeof(DeviceTypes), comboBoxes[i].SelectedItem.ToString(), true); p.deviceTypes.Add(d); p.text.Add(textBoxes[i].Text); } //Delete the current picture and force GC to collect. if (previewPictureBox.Image != null) { previewPictureBox.Image.Dispose(); GC.Collect(); } if (laserPictureBox.Image != null) { laserPictureBox.Image.Dispose(); GC.Collect(); } Image img = new Bitmap(1, 1); //Set the preview image and the hidden laser image previewPictureBox.Image = Wallplate.renderPreview( p.count, p.text.ToArray(), p.deviceTypes.ToArray(), ref img); laserPictureBox.Image = img; previewPictureBox.BringToFront(); showPreviewButton.Enabled = false; showLaserButton.Enabled = true; }
//Worker in a new thread to do the processing in the background //Waits when the queue is at the max queue depth //Ends when the plates queue is empty static void batchProcessor_DoWork(object sender, EventArgs e) { BackgroundWorker worker = (BackgroundWorker)sender; Plate tempPlate; Image laserImage = new Bitmap(1, 1); Image previewImage = new Bitmap(1, 1); //Loop until there are no plates left to work on and the worker hasn't been told to stop. while (plates.Count > 0 && !worker.CancellationPending) { //Loop until the depth limit is reached and the worker hasn't been told to stop. while (laserImages.Count < maxQueueDepth && !worker.CancellationPending) { //Lock the plates queue, dequeue the first plate lock (plates) tempPlate = plates.Dequeue(); //Generate the Images previewImage = Wallplate.renderPreview( tempPlate.count, tempPlate.text.ToArray(), tempPlate.deviceTypes.ToArray(), ref laserImage); //Lock the laser image queue, add the laserImage lock (laserImages) laserImages.Enqueue(laserImage); //Lock the preview image queue, add the previewImage lock (previewImage) previewImages.Enqueue(previewImage); //Report how full the queue is. worker.ReportProgress((int)((double)laserImages.Count / maxQueueDepth * 100.0)); Thread.Sleep(25); } Thread.Sleep(250); } }