not sure what this means, why tid_8.Start(); the thing will loop and start thread 2 that you cant start twice.
sorry, little confused.
if(tid_8.ThreadState != ThreadState.Running)
tid_8.Start();
Also you keep putting thread one, outside the loop it would only run once, it should be in the loop.
and for that matter why use the while loop, the whole thing will loop anyway.(my fault, probably because I changed things and got you confused)
That while loop thing could be so useful, I'm glad you showed it to me, I didn't know you could loop threads like that.
simple code need help
Started by perkunas, Mar 29 2012 04:44 AM
44 replies to this topic
#41
Posted 04 April 2012 - 10:47 AM
#42
Posted 04 April 2012 - 11:09 AM
Sorry, my bad - this doesn't get any easier when I keep messing it up for you.
Your are correct, thread 1 should be inside the loop.
When I said that you can't start a thread twice, I really meant that you can't start a thread if it's already running. So you either join or abort it in order to make sure it's not running anymore before re-starting it in the next loop round. With respect to this, maybe you should join thread 1 lastly in the loop to make sure it can be re-started the next time around.
About thread 8 - at some point you said that this one should run continuously and so I gathered that it could just keep running while the other guys are looping around. Thus, the if-statement is just to make sure you only start thread 8 if it hasn't already been started in a previous loop round.
In summary, maybe something like this would work for you:
Sorry if I was confusing you, hopefully this will clarify things.
Your are correct, thread 1 should be inside the loop.
When I said that you can't start a thread twice, I really meant that you can't start a thread if it's already running. So you either join or abort it in order to make sure it's not running anymore before re-starting it in the next loop round. With respect to this, maybe you should join thread 1 lastly in the loop to make sure it can be re-started the next time around.
About thread 8 - at some point you said that this one should run continuously and so I gathered that it could just keep running while the other guys are looping around. Thus, the if-statement is just to make sure you only start thread 8 if it hasn't already been started in a previous loop round.
In summary, maybe something like this would work for you:
while(true) { tid_1.Start(); tid_2.Start(); tid_3.Start(); tid_4.Start(); tid_5.Start(); tid_2.Join(); tid_3.Join(); tid_4.Join(); tid_5.Join(); tid_6.Start(); tid_7.Start(); tid_7.Join(); tid_6.Abort(); if(tid_8.ThreadState != ThreadState.Running) tid_8.Start(); tid_1.Join(); }
Sorry if I was confusing you, hopefully this will clarify things.
#43
Posted 05 April 2012 - 01:13 AM
well actually thread 8 has nothing in it Id want to have continuously running.
and looking at it, there was some little mistakes so changed it to this. Put two sleeps as it, as it has to sleep a minimum 16 the other number I can use as a variable. So I think I'm going to just leave it as tid_8.Start();
and that's it bobs my uncle.
Wow; now I see it, didn't get the joining thread 1 with 8 they are so far apart missed the thread join thing on the end. The while(true) could even be taken out, and let the whole thing just loop.(start over)
That way I won't have to stop them on next loop. Maybe it wont work that way, IDK. Could put a tid_1.Abort(); after 1 and 8 that would work too I guess,
My idea for changing the variables is to use VB and have it change the variables (text editor)and spit it out as file for upload.
I already have such program I used B4. I'm thinking about trying to rewite it in C# just to learn some thing, might be a little over my head IDK
Anyway many thanks, I learned so much from you, Like I said its my 1st project.
and Id buy you a beer any time.
tnx 4 now
#44
Posted 05 April 2012 - 10:07 AM
looks like this is final
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
namespace controller
{
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 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 controller = new OutputPort(Pins.GPIO_PIN_D8, true);
public static void Main()
{
Thread tid_1 = new Thread(new ThreadStart(Thread_1));
Thread tid_2 = new Thread(new ThreadStart(Thread_2));
Thread tid_3 = new Thread(new ThreadStart(Thread_3));
Thread tid_4 = new Thread(new ThreadStart(Thread_4));
Thread tid_5 = new Thread(new ThreadStart(Thread_5));
Thread tid_6 = new Thread(new ThreadStart(Thread_6));
Thread tid_7 = new Thread(new ThreadStart(Thread_7));
Thread tid_8 = new Thread(new ThreadStart(Thread_8));
while (true)
{
tid_1.Start();
tid_2.Start();
tid_3.Start();
tid_4.Start();
tid_5.Start();
tid_2.Join();
tid_3.Join();
tid_4.Join();
tid_5.Join();
tid_6.Start();
tid_7.Start();
tid_6.Join();
tid_7.Join();
tid_6.Abort();
tid_8.Start();
tid_1.Join();
}
}
public static void Thread_1()
{
drain.Write(false);
Thread.Sleep(16 * MinuteMs);
drain.Write(true);
}
public static void Thread_2()
{
solenoid.Write(false);
Thread.Sleep(16 * MinuteMs);
solenoid.Write(true);
}
public static void Thread_3()
{
a.Write(false);
Thread.Sleep(11 * MinuteMs + 7 * SecondMs);
a.Write(true);
}
public static void Thread_4()
{
b.Write(false);
Thread.Sleep(6 * MinuteMs + 3 * SecondMs);
b.Write(true);
}
public static void Thread_5()
{
ph.Write(false);
Thread.Sleep(3* SecondMs);
ph.Write(true);
}
public static void Thread_6()
{
while (true)
{
controller.Write(false);
Thread.Sleep(1 * SecondMs);
controller.Write(true);
Thread.Sleep(10 * MinuteMs);
}
}
public static void Thread_7()
{
heater.Write(false);
for (int i = 0; i < 14; i++)
{
solenoid.Write(false);
Thread.Sleep(20 * SecondMs);
solenoid.Write(true);
Thread.Sleep(1 * DayMs);
}
heater.Write(true); // added this
}
public static void Thread_8()
{
drain.Write(false);
Thread.Sleep(16 * MinuteMs);
solenoid.Write(false);
Thread.Sleep(16 * MinuteMs);
Thread.Sleep(30 * MinuteMs);
}
}
}
#45
Posted 06 April 2012 - 01:04 AM
doesn't work
on startup I get pins 2 3 4 5 6 firing
thread 1 has pin 5 drain that needs to run 16 min that's it, this task should complete 1st before going on to the next thread.
why is it not doing that?
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users