C#MF线程问题 [英] C# MF threading problem

查看:69
本文介绍了C#MF线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有一些代码,我无法弄清楚.
一切正常,但Method_6和7
无论如何,我无法使其正常工作或正常工作.
必须逆转某些事情
编程的新手,所以我走了这么远真是个奇迹.
任何帮助都很好
谢谢

Ok I have some code and I cant figure it out.
Everything works but Method_6 and 7
No mater what I do I cant get it working or working right.
Something has to be reversed or something
New to programing so it was a miracle that I got this far.
any help would be nice
thanks

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace hydroponic
{
    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 InputPort phcontroller = new InputPort(Pins.GPIO_PIN_D0, false, Port.ResistorMode.Disabled);  //Relay input
        static InputPort nutcontroller = new InputPort(Pins.GPIO_PIN_D1, false, Port.ResistorMode.Disabled);  //Relay input
        static OutputPort ph = new OutputPort(Pins.GPIO_PIN_D2, true);  //optical Relay output (reversed on/off as they are optical)
        static OutputPort b = new OutputPort(Pins.GPIO_PIN_D3, true);  //optical Relay output (reversed on/off as they are optical)
        static OutputPort a = new OutputPort(Pins.GPIO_PIN_D4, true);  //optical Relay output (reversed on/off as they are optical)
        static OutputPort drain = new OutputPort(Pins.GPIO_PIN_D5, true);  //optical Relay output (reversed on/off as they are optical)
        static OutputPort solenoid = new OutputPort(Pins.GPIO_PIN_D6, true);  //optical Relay output (reversed on/off as they are optical)
        static OutputPort controller = new OutputPort(Pins.GPIO_PIN_D7, true);  //optical Relay output (reversed on/off as they are optical)
        static OutputPort led = new OutputPort(Pins.GPIO_PIN_D11, true);  //optical Relay output (reversed on/off as they are optical)


        
        public static void Main()
        {

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

                    method_state_6 = method_state_7 = true;
                    
                       
                    Method_1();
                    call2_5();
                    call6_8();
                    Method_10();
                    Thread.Sleep(10000);

                }
            }
            catch
            {
                //Something has gone wrong; reset to a safe condition
            }
        }


        static void turn_led() //turn led light on for 12 hrs and off for 12hrs in loop.
        {
            while (true)
            {
                led.Write(false);
                Thread.Sleep(HourMs * 12);
                led.Write(true);
                Thread.Sleep(HourMs * 12);
            }
        }

        public static void call2_5()
        {
            Thread t2 = new Thread(new ThreadStart(Method_2));
            t2.Start();
            Thread t3 = new Thread(new ThreadStart(Method_3));
            t3.Start();
            Thread t4 = new Thread(new ThreadStart(Method_4));
            t4.Start();
            Thread t5 = new Thread(new ThreadStart(Method_5));
            t5.Start();
            #region Wait for finishing 2-5 methods
            t2.Join();
            t3.Join();
            t4.Join();
            t5.Join();
            #endregion
        }
        static void call6_8()
        {
            Thread t6 = new Thread(new ThreadStart(Method_6));
            t6.Start();
            Thread t7 = new Thread(new ThreadStart(Method_7));
            t7.Start();
            Thread t9 = new Thread(new ThreadStart(Method_9));
            t9.Start();

            // wait for Method_9 finishing
            t9.Join();
            method_state_6 = method_state_7 = false;
        }
        public static void Method_1() //Drains the System (water nutrient mix)
        {
            drain.Write(false);
            Thread.Sleep(16 * MinuteMs);
            drain.Write(true);
        }
        public static void Method_2() //Fills the System with Water
        {
            solenoid.Write(false);
            Thread.Sleep(16 * MinuteMs);
            solenoid.Write(true);
        }
        public static void Method_3() //Doses the system with nutrient a
        {
            a.Write(false);
            Thread.Sleep(182 * SecondMs);
            a.Write(true);
        }
        public static void Method_4() //Doses the system with nutrient b
        {
            b.Write(false);
            Thread.Sleep(100 * SecondMs);
            b.Write(true);
        }
        public static void Method_5() //Doses the system with PH down
        {
            ph.Write(false);
            Thread.Sleep(5 * SecondMs);
            ph.Write(true);
        }
        public static void Method_6() // reads the PH controller realy contactcts if opend/closed when closed it does this
        {
            while (phcontroller.Read() == true && method_state_6 == true)
            {
                ph.Write(false);
                Thread.Sleep(1 * SecondMs);
                ph.Write(true);
                Thread.Sleep(5 * MinuteMs);
            }
        }
        public static void Method_7() // reads the nutrient controller realy contactcts if opend/closed when closed it does this
        {
            while (nutcontroller.Read() == true && method_state_7 == true)
            {
                a.Write(false);
                Thread.Sleep(5454);
                a.Write(true);
                b.Write(false);
                Thread.Sleep(3000);
                b.Write(true);
                Thread.Sleep(5 * MinuteMs);
            }
        }
        public static void Method_9() //Tops up the system with a little water each day, then in 14 days moves on to Method_10
        {
            controller.Write(false); //turns on the two controllers PH and Nutrients
            for (int i = 0; i < 14; i++)
            {
                Thread.Sleep(1 * DayMs);
                solenoid.Write(false);
                Thread.Sleep(20 * SecondMs);
                solenoid.Write(true);

            }
            controller.Write(true);
        }
        public static void Method_10() // Flushes the system, drains then fill water.
        {
            drain.Write(false);
            Thread.Sleep(16 * MinuteMs);
            drain.Write(true);
            solenoid.Write(false);
            Thread.Sleep(16 * MinuteMs);
            solenoid.Write(true);
            Thread.Sleep(1 * MinuteMs);
          
        }
                         //Whole thing loops
    }
}

推荐答案

我已经尝试过了
while(phcontroller.Read()== true&&method_state_6 == true)
这似乎有效,但相反,但稍停后会停止
然后我尝试添加!
while(!phcontroller.Read()== true&&method_state_6 == true)
这似乎只能工作一次.

这似乎几乎是硬件问题,但我已经检查了很多次.
没有错误消息,只有程序无法正常运行.
method_6和7 Wont循环最多尝试一次或两次.
它是软件问题,我已经运行了测试程序
效果很好


Well I have tried
while (phcontroller.Read() == true && method_state_6 == true)
This seems to work but in reverse but stops after a bit
then I tried to add the !
while (!phcontroller.Read() == true && method_state_6 == true)
This seems to work only one time.

It almost seems like its a hardware issue, but Ive checked that many times.
no error messages only a program not working right.
method_6 and 7 Wont loop at best tries once or twice.
Its a software issue I have run a test program
that works fine


public static void Main()
        {
            bool buttonState = false;
            while (true)
            {
                buttonState = phcontroller.Read();
                ph.Write(buttonState);
                a.Write(buttonState);
                b.Write(buttonState);
               
        }
    }
    }
}


这篇关于C#MF线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆