我是编程和寻找一个简单的解决方案的新手,那是行不通的 [英] I'm new to programing and looking at a simple solution, thats not working

查看:73
本文介绍了我是编程和寻找一个简单的解决方案的新手,那是行不通的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个线程,两个都设置为同时运行。

第一个是while循环,我正在尝试int p = 1;那么0

所以在我的下一个帖子中

如果p == 0做这个

新的不确定这是否有效,是分为两个主题。





  public  静态  void  Method_8()
{
while (method_state_6&& method_state_7 == true
{
int p = 1 ;
flooddrain.Write( false );
Thread.Sleep( 15 * MinuteMs); // 泛滥表时间
flooddrain.Write( true );
Thread.Sleep( 1 * HourMs); // 排水表时间
int p = 0 ;
Thread.Sleep( 1 * MinuteMs); // 记住充值时间
}
}

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 );
}

解决方案

m。
好​​的......我们走了......

你正在改变和检查的变量是不同的变量 - 它们之间没有任何关系。

  public   static   void  Method_8()
{
while (method_state_6&& method_state_7 == true
{
int p = 1 ;
flooddrain.Write( false );
Thread.Sleep( 15 * MinuteMs); // 泛滥表时间
flooddrain.Write( true );
Thread.Sleep( 1 * HourMs); // 排水表时间
int p = 0 ;
Thread.Sleep( 1 * MinuteMs); // 记住充值时间
}
}

写的时候

  int  p =  1 ; 

您声明一个全新的局部变量,该变量仅存在于该方法中,并且不能在其外部访问。我们可以忽略第二个声明,因为它不会在那里编译,即使你这次将它设置为零。

  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 );
}

这个方法使用了一个不同的 p ,这可能是(在某种程度上)在类级别声明的,但是无论如何都没有任何关系。 Method_8中的一个



如果你想做这样的事情,那么你需要确保两个线程都在查看相同的变量(并且优先使用锁让它更安全一点)


这只是一个关于如何并行运行两个线程的示例,你必须修改这个方法来比较你的问题。您必须注意,要在线程之间共享资源/数据,它应该作为成员可用,否则它们将处理不同的不同数据。



在你的情况下,变量p在方法中定义,它变成该方法的局部变量,因此在一个方法中设置P对另一个方法没有任何影响

<前lang =cs> int p = 0 ; // 将此作为成员变量
bool thread1Alive = true ;
bool thread2Alive = false ;

private void StartThread()
{
线程thread1 = new 线程( new ThreadStart(ThreadProcedure1));
线程thread2 = new 线程( new ThreadStart(ThreadProcedure2));

thread1.Start();
thread2.Start();
}

private void ThreadProcedure1()
{
while (thread1Alive) // this while循环将使你的线程保持活着,直到thread1Alive为真;
{
while (method_state_6&& method_state_7 == true
{
p = 1 ;
flooddrain.Write( false );
Thread.Sleep( 15 * MinuteMs); // 泛滥表时间
flooddrain.Write( true );
Thread.Sleep( 1 * HourMs); // 排水表时间
p = 0 < /跨度>;
Thread.Sleep( 1 * MinuteMs); // 记住充值时间
}
}
}

private void ThreadProcedure2()
{
< span class =code-keyword> while
(thread2Alive)
{
if (p == 0
{
// 做点什么
}
Thread.Sleep(someValue); //
}
}

私有 void CloseThreads()
{
thread1Alive = false ;
thread2Alive = false ;
}


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); 
        }

解决方案

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)


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;
}


这篇关于我是编程和寻找一个简单的解决方案的新手,那是行不通的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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