Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two threads, both are set to run same time.
1st one is in a while loop, I'm trying to do int p = 1; then 0
so in my next thread
if p==0 do "this"
Being new not sure if this even works, being in two threads and all.


C#
public static void Method_8()
        {
            while (method_state_6 && method_state_7  == true)
            {
                int p = 1;
                flooddrain.Write(false);
                Thread.Sleep(15 * MinuteMs);//flooding table time
                flooddrain.Write(true);
                Thread.Sleep(1 * HourMs); //Draining table time
                int p = 0;
                Thread.Sleep(1 * MinuteMs); //Remember time for topup
            }
        }

         public static void Method_11()
        {
            heater.Write(false);
            for (int i = 0; i < 14; i++)
            {
                Thread.Sleep(1 * DayMs);
                If (p == 0);
                {
                solenoid.Write(false);
                Thread.Sleep(20 * SecondMs);
                solenoid.Write(true);
                }

            }
            heater.Write(true); 
        }
Posted
Comments
Jibesh 7-Jan-13 15:05pm    
can you reframe your questions its really hard to understand your need from what you typed here.
Two Threads , one thread will be setting P==1, then 0 when P==0 is set another thread start processing is that what you want?
ve3tru 7-Jan-13 15:13pm    
Yes it is in a separate thread, just trying to have one thread sink with the other
at that point. when P==0 do this.....
I'm sure I even wrote some thing incorrect.
Like I said I'm new, so please help.
Jibesh 7-Jan-13 15:18pm    
is your thread keep running for ever or it dies once it completes the operations?
ve3tru 7-Jan-13 15:29pm    
the
(int i = 0; i < 14; i++) counts 14 days
this is the last thread then the whole thing loops
It counts one day,then when p=0 "Do this", so that's just so the two threads sink together. guess the only other way is date time thing, but more work.
The second thread needs a loop, to check when p==0, that looks wrong too.
ve3tru 7-Jan-13 15:39pm    
"If you want to do something like this, then you need to make sure that both threads are looking at the same variable (and by preference use a lock to make it all a bit safer)"
Ok how do I do that, one thread keeps changing the variable, and I want the other to read the new variable.

Is this even possible?.

m. Ok...here we go...
The variables you are changing and checking are different variables - they are not related in any way to each other.
C#
public static void Method_8()
{
    while (method_state_6 && method_state_7  == true)
    {
        int p = 1;
        flooddrain.Write(false);
        Thread.Sleep(15 * MinuteMs);//flooding table time
        flooddrain.Write(true);
        Thread.Sleep(1 * HourMs); //Draining table time
        int p = 0;
        Thread.Sleep(1 * MinuteMs); //Remember time for topup
    }
}
When you write
C#
int p = 1;
you declare a brand new, local variable that exists only within the method, and cannot be accessed outside it. We can ignore the second declaration since it won't compile with that in there anyway, even if you are setting it to zero this time.
C#
public static void Method_11()
{
    heater.Write(false);
    for (int i = 0; i < 14; i++)
    {
        Thread.Sleep(1 * DayMs);
        If (p == 0);
        {
        solenoid.Write(false);
        Thread.Sleep(20 * SecondMs);
        solenoid.Write(true);
        }

    }
    heater.Write(true);
}
This method uses a different p which is (presumably) declared at class level, but which has nothing to do whatsoever with the one in Method_8

If you want to do something like this, then you need to make sure that both threads are looking at the same variable (and by preference use a lock to make it all a bit safer)
 
Share this answer
 
Comments
Philip Stuyck 7-Jan-13 15:33pm    
right answer, the example provided by the user does not even compile.
If he is really new to programming, then jumping in to multithreading is really a bad idea. That is advanced stuff.
ve3tru 7-Jan-13 15:54pm    
Looks like having one thread changing a variable, and getting the other one to read it, is way too hard, well for me anyway.
This is just a sample on how to run two threads parallel , you have to modify this method comparing to your problem. You must note that to share a resource/data between the threads it should be available as members to both otherwise they will be working on a different different data.

In your case the variable p is defined inside the method which becomes a local variable to that method so setting P inside one method doesnt have any effect on the other method
C#
int p=0; // make this as member variable
bool thread1Alive = true;
bool thread2Alive = false;

private void StartThread()
{
  Thread thread1 = new Thread(new ThreadStart(ThreadProcedure1));
  Thread thread2 = new Thread(new ThreadStart(ThreadProcedure2));
 
  thread1.Start();
  thread2.Start();
}

private void ThreadProcedure1()
{
 while(thread1Alive) // this while loop will keep your thread alive for ever untill thread1Alive true;
 {
   while (method_state_6 && method_state_7  == true)
   {
      p = 1;
      flooddrain.Write(false);
      Thread.Sleep(15 * MinuteMs);//flooding table time
      flooddrain.Write(true);
      Thread.Sleep(1 * HourMs); //Draining table time
      p = 0;
      Thread.Sleep(1 * MinuteMs); //Remember time for topup
     }   
   }
}

private void ThreadProcedure2()
{
 while(thread2Alive)
 {
   if( p==0)
   {
     //do something
   }
   Thread.Sleep(someValue);//
 }
}

private void CloseThreads()
{
 thread1Alive = false;
 thread2Alive = false;
}
 
Share this answer
 
v2
Comments
ve3tru 7-Jan-13 16:39pm    
Thanks, but I already have that, in fact I have 6 threads running same time parallel. My fault I just gave you a snip-it of code, the rest is hidden so you couldn't see it. I was trying to modify a variable in one thread and get the other one to read it. The only other way I can think of doing this is Time stamping it."In your case the variable p is defined inside the method which becomes a local variable to that method so setting P inside one method doesn't have any effect on the other method."
So in other words I cant get there from here lol
Jibesh 7-Jan-13 16:45pm    
Never "P" as a local variable!!! but there are other ways you can get share the variable across the threads. Just i mentioned in my solution i.e keep P as member variable, or a static object which is accessed by the threads.

why cant you define P as member?
ve3tru 7-Jan-13 16:58pm    
why cant you define P as member?
Please do explain what you mean example?.
P.S.

Might be a dumb question
but my whole thread is public static

public static void Method_11()

so why cant it pass the info

bear with me I'm learning
Jibesh 7-Jan-13 17:45pm    
Your method is public doesnt mean that all the variables declared inside the method is publicly accessible, I hope you understand the meaning what is local variable and member variable, I understand you are learning but these all the the very basic thing you must know before you start programming. You must be strong enough with the fundamentals to become a good programmer.
ve3tru 7-Jan-13 17:32pm    
I tried it as a bool
static bool p = false;


If(p == true);

that compiles but I don't think its right

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