05-16-2016 08:15 AM
I'm working on a search program and would like to have a national instruments led control blinking during the search. The led needs to be started on its own thread while the search runs on a separate thread. This is my first attempt at multi-threading in c# and I'm not sure what to try.
Here is my c# code, I'm trying to start the LED blinking then run the search then stop the LED blinking. The LEDON() and LEDOFF() work if run separately.
private void btnSearch_Click(object sender, EventArgs e) { Thread LEDStart = new Thread(LEDON); //Creates a Thread that will call LEDON() LEDStart.Start(); int i; string temp = "*" + textBoxFileName.Text + "*"; string[] AllFiles = Directory.GetFiles(this.textBoxFolderpath.Text, temp, SearchOption.AllDirectories); for (i = 0; i < AllFiles.Length - 1; i++) { progressBar1.Maximum = AllFiles.Length - 1; string[] row = new string[] { Path.GetFileName(AllFiles[i]), AllFiles[i] }; dataGridView1.Rows.Add(row); progressBar1.Increment(1); if (progressBar1.Value == progressBar1.Maximum) { Thread LEDEND = new Thread(LEDOFF); //Creates a Thread that will call LEDOFF() LEDEND.Start(); } } } //Turn On LED private void LEDON() { if (this.led1.InvokeRequired) { this.led1.Invoke(new MethodInvoker(() => LEDON())); return; }
05-17-2016 05:18 AM
Looks like my code got cut off, here is the complete code:
private void btnSearch_Click(object sender, EventArgs e) { Thread LEDStart = new Thread(LEDON); //Creates a Thread that will call LEDON() Thread.CurrentThread.Name = "LEDON"; LEDStart.Start(); int i; string temp = "*" + textBoxFileName.Text + "*"; string[] AllFiles = Directory.GetFiles(this.textBoxFolderpath.Text, temp, SearchOption.AllDirectories); for (i = 0; i < AllFiles.Length - 1; i++) { progressBar1.Maximum = AllFiles.Length - 1; string[] row = new string[] { Path.GetFileName(AllFiles[i]), AllFiles[i] }; dataGridView1.Rows.Add(row); progressBar1.Increment(1); if (progressBar1.Value == progressBar1.Maximum) { Thread LEDEND = new Thread(LEDOFF); //Creates a Thread that will call LEDOFF() //Thread.CurrentThread.Name = "LEDOFF"; LEDEND.Start(); } } } //Turn On LED public void StartLEDON() { Thread LEDStart = new Thread(LEDON); //Creates a Thread that will call LEDON() //Thread.CurrentThread.Name = "LEDON"; LEDStart.Start(); } public void LEDON() { if (this.led1.InvokeRequired) { this.led1.Invoke(new MethodInvoker(() => LEDON())); return; } led1.BlinkMode = NationalInstruments.UI.LedBlinkMode.BlinkWhenOn; led1.Value = true; } //Turn Off LED public void LEDOFF() { if (this.led1.InvokeRequired) { this.led1.Invoke(new MethodInvoker(() => LEDOFF())); return; } //led1.BlinkMode = NationalInstruments.UI.LedBlinkMode.BlinkWhenOn; led1.Value = false; }
05-19-2016 04:28 PM
Have you taken a look at the multithreading whitepaper that goes over multithreading in CVI?
http://www.ni.com/white-paper/3663/en/
Additionally, there is an example that goes through how to do multithreading in CVI.
http://www.ni.com/example/29662/en/
Do you have a more specific questoin?