Visual studio button problem

Thread Starter

Dritech

Joined Sep 21, 2011
901
Hi all,

I am using a button will be used to blink an LED. When this button is pressed, I am turning on the LED for 5 seconds, and then turn it off for 5 seconds. The problem is that I want the LED to keep blinking as long as the button is pressed.
How can I achieve this please? I tried using both BUTTON CLICK and BUTTON DOWN but non of them worked (function only executed once). Is there a method that as long as the button is pressed, the program keeps looping in that function?
Note, I am programming using Csharp.

Thanks in advance for your help.
 

Thread Starter

Dritech

Joined Sep 21, 2011
901
It is software combined with hardware. Below is the part of the code:

Rich (BB code):
        private void Button_up_MouseDown(object sender, MouseEventArgs e) 
        {
            string Send_code = "a";
            serial(use_COM, Send_code);
        }
Now I want the program to keep sending the character a as long as the button is pressed and not just send it once.
 

tshuck

Joined Oct 18, 2012
3,534
It is software combined with hardware. Below is the part of the code:

Rich (BB code):
        private void Button_up_MouseDown(object sender, MouseEventArgs e) 
        {
            string Send_code = "a";
            serial(use_COM, Send_code);
        }
Now I want the program to keep sending the character a as long as the button is pressed and not just send it once.
Why not let the hardware count the 5 seconds and just send go/stop commands when the button state changes?
 

Thread Starter

Dritech

Joined Sep 21, 2011
901
Thanks for the reply.
Can I use a while look with the MOUSE UP?

Rich (BB code):
        private void Button_up_MouseDown(object sender, MouseEventArgs e)       
           {
           while(mouseUP)
            {
            string Send_code = "a";            
            serial(use_COM, Send_code);
            }
           }
 

tshuck

Joined Oct 18, 2012
3,534
Thanks for the reply.
Can I use a while look with the MOUSE UP?

Rich (BB code):
        private void Button_up_MouseDown(object sender, MouseEventArgs e)       
           {
           while(mouseUP)
            {
            string Send_code = "a";            
            serial(use_COM, Send_code);
            }
           }
If you do that, you'll lock up your application.
 

Brownout

Joined Jan 10, 2012
2,390
You can spawn off a thread to blink the LED. Create the thread on the first click event and pass a parameter to tell it to blink or not. On subsequent events, first check to see if the thread has already been created, and if so, just pass a message to blink or not blink. It might sound complicated, but it's very easy.
 
Top