Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Trying to add an event handler its actually a switch to my program.(hope to add 2)
But its crashing runs like 3 seconds stops.
seems to crash around this point "if ((System.DateTime.Now.Ticks - lastDebounceTime) > debounceDelay)"

please help

C#
namespace Hydro
{
    public class Program
    {
        public const int SecondMs = 1000;
        public const int MinuteMs = 60 * SecondMs;
        public const int HourMs = 60 * MinuteMs;
        public const int DayMs = 24 * HourMs;
        public const int WeekMs = 7 * DayMs;

        static bool method_state_6, method_state_7;
        static long lastDebounceTime = DateTime.Now.Ticks;
        static long debounceDelay = 100000;   //adjust as necessary
        
        
        static InputPort phcontroller = new InputPort(Pins.GPIO_PIN_D0, false, Port.ResistorMode.Disabled);
        static InputPort nutcontroller = new InputPort(Pins.GPIO_PIN_D1, false, Port.ResistorMode.Disabled);
        static OutputPort ph = new OutputPort(Pins.GPIO_PIN_D2, true);
        static OutputPort b = new OutputPort(Pins.GPIO_PIN_D3, true);

        static OutputPort a = new OutputPort(Pins.GPIO_PIN_D4, true);
        static OutputPort drain = new OutputPort(Pins.GPIO_PIN_D5, true);
        static OutputPort solenoid = new OutputPort(Pins.GPIO_PIN_D6, true);
        static OutputPort heater = new OutputPort(Pins.GPIO_PIN_D7, true);
        
        static OutputPort led = new OutputPort(Pins.GPIO_PIN_D11, true);


        
        public static void Main()
        {
		InterruptPort input = new InterruptPort(Pins.GPIO_PIN_D10, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
        	input.OnInterrupt += new NativeEventHandler(input_OnInterrupt);
	}

        static void input_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            if ((System.DateTime.Now.Ticks - lastDebounceTime) > debounceDelay)
            {
                lastDebounceTime = DateTime.Now.Ticks;
                // do stuff
            }
            {

                try
                {
                    Thread led_proc = new Thread(new ThreadStart(turn_led));
                    led_proc.Start();
                    while (true)
                    {

                        Method_1();
                        call2_5();
                        call6_8();
                        Method_9();
                        Thread.Sleep(10000);
Posted
Comments
Sergey Alexandrovich Kryukov 6-Jan-13 23:08pm    
What does it mean "around this point". You need to start with finding exact like where an expression is throw.
Generally, software developers don't say "crash".
—SA
Jibesh 7-Jan-13 3:48am    
Check the possibility of Christian Graus solution. that can also be a reason for your error. Or post the exact line where you are having trouble.
ve3tru 7-Jan-13 6:09am    
"What does it mean "around this point"
Well on debug, it starts then it stops after like 3 seconds. Its like the whole program is finished. No errors no expression is throw or problems, and no more debug, so its difficult to gauge the exact point. The too many thread thing, is probable. From what I understand, is others have run up to 50 without a problem, it just gets real slow.(Total threads might be high but I'm only running like 3 same time) What I was told, is that in theory, you can have as many threads as you want, but it will slow it down to an unusable speed,I'm not having that problem.
I have tried many versions of this code, they all fail.I was thinking opening a new thread and use a while loop instead of the event handler but some how I think that's just more resource hungry.

Might it be crashing because it just creates more and more threads until it's run out of the ability to do so ? Each interrupt creates a thread, which will run forever.
 
Share this answer
 
I think I solved it, it had to do with creating even more threads well at least ones
in a specific order of things.
Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900