Last active
December 15, 2015 06:59
-
-
Save hyounggyu/5219745 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.ComponentModel; | |
| using System.Data; | |
| using System.Drawing; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Windows.Forms; | |
| using NationalInstruments.Vision; | |
| using NationalInstruments.Vision.Acquisition.Imaq; | |
| namespace StroboR3 | |
| { | |
| public partial class Form1 : Form | |
| { | |
| private Acquisition acquisition = null; | |
| private System.ComponentModel.BackgroundWorker acquisitionWorker; | |
| private struct UIUpdateArgs | |
| { | |
| public uint bufferNumber; | |
| public string pixelValue; | |
| public UIUpdateArgs(uint _bufferNumber, string _pixelValue) | |
| { | |
| bufferNumber = _bufferNumber; | |
| pixelValue = _pixelValue; | |
| } | |
| } | |
| public Form1() | |
| { | |
| InitializeComponent(); | |
| // Initialize the UI. | |
| startButton.Enabled = true; | |
| stopButton.Enabled = false; | |
| quitButton.Enabled = true; | |
| // Set up the acquisition background worker thread. This thread | |
| // will actually acquire the images and issue a callback | |
| // to update the UI. | |
| acquisitionWorker = new System.ComponentModel.BackgroundWorker(); | |
| acquisitionWorker.DoWork += new DoWorkEventHandler(acquisitionWorker_DoWork); | |
| acquisitionWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(acquisitionWorker_RunWorkerCompleted); | |
| acquisitionWorker.ProgressChanged += new ProgressChangedEventHandler(acquisitionWorker_ProgressChanged); | |
| acquisitionWorker.WorkerReportsProgress = true; | |
| acquisitionWorker.WorkerSupportsCancellation = true; | |
| } | |
| private void startButton_Click(object sender, EventArgs e) | |
| { | |
| try | |
| { | |
| // Update the UI. | |
| startButton.Enabled = false; | |
| stopButton.Enabled = true; | |
| acquisition = new Acquisition(); | |
| acquisition.Setup(); | |
| acquisitionWorker.RunWorkerAsync(); | |
| } | |
| catch (ImaqException ex) | |
| { | |
| MessageBox.Show(ex.Message, "NI-IMAQ Error"); | |
| Cleanup(); | |
| } | |
| } | |
| void acquisitionWorker_DoWork(object sender, DoWorkEventArgs e) | |
| { | |
| // This is the main function of the acquisition background worker thread. | |
| // Perform image processing here instead of the UI thread to avoid a | |
| // sluggish or unresponsive UI. | |
| BackgroundWorker worker = (BackgroundWorker)sender; | |
| try | |
| { | |
| // Loop until we tell the thread to cancel or we get an error. When this | |
| // function completes the acquisitionWorker_RunWorkerCompleted method will | |
| // be called. | |
| while (!worker.CancellationPending) | |
| { | |
| acquisition.Run(imageViewer1.Image, imageViewer2.Image); | |
| // Update the UI by calling ReportProgress on the background worker. | |
| // This will call the acquisition_ProgressChanged method in the UI | |
| // thread, where it is safe to update UI elements. Do not update UI | |
| // elements directly in this thread as doing so could result in a | |
| // deadlock. | |
| //worker.ReportProgress(0, new UIUpdateArgs(bufferNumber, pixelValue)); | |
| //bufferNumber++; | |
| } | |
| } | |
| catch (ImaqException ex) | |
| { | |
| // If an error occurs and the background worker thread is not being | |
| // cancelled, then pass the exception along in the result so that | |
| // it can be handled in the acquisition_RunWorkerCompleted method. | |
| if (!worker.CancellationPending) | |
| e.Result = ex; | |
| } | |
| } | |
| void acquisitionWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) | |
| { | |
| // Update the UI with the information passed from the background worker thread. | |
| UIUpdateArgs updateArgs = (UIUpdateArgs)e.UserState; | |
| //bufNumTextBox.Text = updateArgs.bufferNumber.ToString(); | |
| //pixelValTextBox.Text = updateArgs.pixelValue; | |
| } | |
| void acquisitionWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) | |
| { | |
| // The background worker thread has completed its execution. Perform any cleanup here. | |
| if (e.Result is ImaqException) | |
| { | |
| // If we get here it means that we had an error in the background worker thread | |
| // that we need to handle. | |
| MessageBox.Show(((ImaqException)e.Result).ToString(), "NI-IMAQ Error"); | |
| } | |
| Cleanup(); | |
| } | |
| private void stopButton_Click(object sender, EventArgs e) | |
| { | |
| try | |
| { | |
| if (acquisition != null) | |
| { | |
| // Signal the background worker thread to stop and then stop the acquisition. | |
| acquisitionWorker.CancelAsync(); | |
| acquisition.Stop(); | |
| } | |
| } | |
| catch (ImaqException ex) | |
| { | |
| MessageBox.Show(ex.Message, "NI-IMAQ Error"); | |
| } | |
| } | |
| private void quitButton_Click(object sender, EventArgs e) | |
| { | |
| // Clean up and exit. | |
| stopButton.PerformClick(); | |
| this.Close(); | |
| Application.Exit(); | |
| } | |
| private void Cleanup() | |
| { | |
| if (acquisition != null) | |
| { | |
| // Close the session. | |
| acquisition.Close(); | |
| acquisition = null; | |
| } | |
| // Update the UI. | |
| startButton.Enabled = true; | |
| stopButton.Enabled = false; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment