
simple code need help
#1
Posted 29 March 2012 - 04:44 AM
#2
Posted 29 March 2012 - 08:49 AM
To my knowledge macros are not available in C#. I don't think there is a preprocessor in the same way as in C/C++....in C++ I would use macros...
You should be able to fork up the threads and then issue Thread.Join(...) on each of them from your main routine to make sure they're all finished before proceeding. I even think you can pass a timeout parameter to limit the wait....wait till they are all finished before continuing on (variable times)not sure the best way to tackle that one.
#3
Posted 29 March 2012 - 11:41 AM
Hanzibal is right, there is no direct equivalent of C/C++ preprocessor macros in C#. Probably the closest solution would be something like this:1) question how do I define hours minutes weeks ect.
in C++ I would use macros some thing like this
const int SecondMs = 1000; const int MinuteMs = 60*SecondsMs; const int HourMs = 60*MinutesMs; const int DayMs = 24*HourMs; const int WeekMs = 7*DayMs; Thread.Sleep(16*MinuteMs); Thread.Sleep(2*DayMs + 6*HourMs);Compiler will replace the constant expression with the resulting value, which is the same as for C/C++ macro. Alternatively, you could also create methods that mimic the macros (i.e. take a parameter and multiply it with 'milliseconds' constant) or use TimeSpan structure (see its constructor, Ticks property and TicksPerXXX fields).
Please note that type of the millisecondsTimeout parameter in Thread.Sleep() is only int (vs. unsigned long in the macros), so it is limited to less than 25 days.
#4
Posted 29 March 2012 - 12:49 PM
#5
Posted 29 March 2012 - 01:11 PM
public class Program { const int SecondMs = 1000; const int MinuteMs = 60*SecondsMs; const int HourMs = 60*MinutesMs; const int DayMs = 24*HourMs; const int WeekMs = 7*DayMs; public static void Main() { // ... } }
EDIT: There are extra 's' on the second and third line, 60*SecondsMs should be SecondMs and similarly, 60*MinutesMs should be MinuteMs. Sorry.
#6
Posted 29 March 2012 - 02:39 PM
#7
Posted 29 March 2012 - 03:00 PM
... const int SecondMs = 1000; const int MinuteMs = 60 * SecondMs; // <- extra 's' was here const int HourMs = 60 * MinuteMs; // <- extra 's' was here too const int DayMs = 24 * HourMs; const int WeekMs = 7 * DayMs;Also, if you want to use the constant from outside the Program class, you'd need to declare them as 'public const ...' (or internal).
#8
Posted 29 March 2012 - 03:17 PM
#9
Posted 29 March 2012 - 04:18 PM
There is no need to rename the Program class, you can just leave it as it was (in your previous post). I was referring to making the constants public, such as public const int SecondMs = 1000;, sorry for the confusion. C# has similar access modifiers like C++, class members are 'private' by default (when no modifier is specified). Unlike C++, there is no notation of global variables or constants in C#, they must belong to a class (or they can be declared as enum etc.).is this right, it compiles and all and I named it public class constant although that doesn't seem right
#10
Posted 29 March 2012 - 04:45 PM
#11
Posted 29 March 2012 - 08:24 PM
#12
Posted 30 March 2012 - 06:19 AM
I guess you can use TimeSpan like this..I was just wondering why not simply use the Thread.Sleep("TimeSpan") overloaded method, but then noticed it's missing in .net MF grr! :-)
// 1 day and 30 minutes Thread.Sleep((int)((new TimeSpan(1, 0, 30, 0)).Ticks/TimeSpan.TicksPerMillisecond));It would be a little bit simpler if it had TotalMilliseconds property, like the 'full framework' version has.
#13
Posted 31 March 2012 - 02:28 PM
#14
Posted 31 March 2012 - 02:41 PM
I guess you mean to do something like below, that way you will have 4 threads running simultaneously (almost) and then wait until they're all finished (16 minutes).I need all of these things to happen at the same time, wait till they are all done then continue.
public class Program { 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)); tid_1.Start(); tid_2.Start(); tid_3.Start(); tid_4.Start(); tid_1.Join(); tid_2.Join(); tid_3.Join(); tid_4.Join(); } public static void Thread_1() { solenoid.Write(false); Thread.Sleep(16 * MinuteMs); solenoid.Write(true); } public static void Thread_2() { a.Write(false); Thread.Sleep(3 * SecondMs); a.Write(true); } public static void Thread_3() { b.Write(false); Thread.Sleep(6 * MinuteMs + 3 * SecondMs); b.Write(true); } public static void Thread_4() { ph.Write(false); Thread.Sleep(11 * MinuteMs + 7 * SecondMs); ph.Write(true); } }
#15
Posted 31 March 2012 - 03:00 PM
#16
Posted 31 March 2012 - 05:27 PM
public class Program { public static void Main() { solenoid.Write(false); a.Write(false); b.Write(false); ph.Write(false); Thread.Sleep(3 * SecondMs); // 3 seconds in total a.Write(true); Thread.Sleep(6 * MinuteMs); // 6 minutes and 3 seconds in total b.Write(true); Thread.Sleep(5 * MinuteMs + 4 * SecondMs); // 11 minutes and 7 seconds in total ph.Write(true); Thread.Sleep(4 * MinuteMs + 53 * SecondMs); // 16 minutes in total solenoid.Write(true); } }Or maybe this was what you had in the first place?
Setting up a graphical interface would require so much more work - don't forget, your on a microcontroller now, not a PC. So you'll need an LCD with touch sensor (or physical knobs) and then write a graphical library to go with it all.
#17
Posted 31 March 2012 - 08:28 PM
#18
Posted 31 March 2012 - 09:39 PM
#19
Posted 31 March 2012 - 11:12 PM
#20
Posted 31 March 2012 - 11:23 PM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users