How to multithread to start a method

jrdnoland

Member
Joined
May 15, 2016
Messages
9
Programming Experience
10+
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.

C#:
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;
            }
 
            led1.BlinkMode = NationalInstruments.UI.LedBlinkMode.BlinkWhenOn;
            led1.Value = true;         
        }
        //Turn Off LED  
        private void LEDOFF()
        {
            if (this.led1.InvokeRequired)
            {
                this.led1.Invoke(new MethodInvoker(() => LEDOFF()));
                return;
            }
            //led1.BlinkMode = NationalInstruments.UI.LedBlinkMode.BlinkWhenOn;
            led1.Value = false;           
        }
 
What you could do is right after you start the processing thread enable a timer that would do the blinking, then when the thread ends just disable the timer and set the LED to whatever state it needs to be.
 
What you could do is right after you start the processing thread enable a timer that would do the blinking, then when the thread ends just disable the timer and set the LED to whatever state it needs to be.

I'm not sure if that would make a difference. Isn't starting the led based on a timer the same sort of concept? Is my code correct, in starting the threads?
 
As a Control InvokeRequired will return true, Invoke would then wait for UI thread to allow it to process, but UI thread in your code is busy with the button event handler code.
 
As a Control InvokeRequired will return true, Invoke would then wait for UI thread to allow it to process, but UI thread in your code is busy with the button event handler code.

How do I make it so that the led thread will finish separately from the UI thread. I thought that was the point of threading?
 
Controls are not designed to run in separate threads, they're meant to operate in same thread as the form they are on. It is other processing that you have to put in separate threads, in order to not disturb the normal operations of the UI. For example Directory.GetFiles is something that may block UI for a while and should be done on secondary thread.
 
Controls are not designed to run in separate threads, they're meant to operate in same thread as the form they are on. It is other processing that you have to put in separate threads, in order to not disturb the normal operations of the UI. For example Directory.GetFiles is something that may block UI for a while and should be done on secondary thread.

I didn't realize that, I will try to run the search in another thread.

Thanks for the information!
 

Latest posts

Back
Top Bottom