Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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.

 
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 7-Jan-13 8:56am
ve3tru219
Comments
jibesh at 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 at 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 at 7-Jan-13 15:18pm
   
is your thread keep running for ever or it dies once it completes the operations?
ve3tru at 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 at 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?.
jibesh at 7-Jan-13 15:55pm
   
Check my solution for how to write 2 threads running parallel, there are other ways you can control the threads let me know if you want to hear more
Philip Stuyck at 8-Jan-13 0:40am
   
You should read something about thread synchronisation. This should answer your problems.
Sergey Alexandrovich Kryukov at 23-Jan-13 15:29pm
   
Please don't re-post anymore. It cannot help you, will only cause abuse reports.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
        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
                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.
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)
  Permalink  
Comments
Philip Stuyck at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
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;
}
  Permalink  
v2
Comments
ve3tru at 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 at 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 at 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 at 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 at 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
ve3tru at 7-Jan-13 18:05pm
   
I think I got it, if (p==true)
 
anyway used a bool it compiles, whether it will cross from one thread to the other
I guess I will need to check out.
 
Thanks
jibesh at 7-Jan-13 18:07pm
   
Good to see that you solved your problem.
jibesh at 7-Jan-13 18:07pm
   
was this variable defined as member to the class where your threads are defined?
Sergey Alexandrovich Kryukov at 23-Jan-13 15:31pm
   
First of all, "thread alive" variables should be accessed using lock statement.
And the purpose of such code is questionable...
—SA

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 510
1 CPallini 330
2 OriginalGriff 215
3 George Jonsson 120
4 ChauhanAjay 104
0 OriginalGriff 6,464
1 Sergey Alexandrovich Kryukov 5,915
2 CPallini 5,175
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100