Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

How to multithread to start a method that controls an led

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.

Code:
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;
            }
0 Kudos
Message 1 of 3
(3,299 Views)

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;           
        }
0 Kudos
Message 2 of 3
(3,285 Views)

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?

-----------------------------------------------
Brandon Grey
Certified LabVIEW Architect

0 Kudos
Message 3 of 3
(3,256 Views)