Burak Koçyiğit
Burak Koçyiğit

Reputation: 82

Best way to wait until a condition is true

I want program to wait when a variable come from plc. I have tried a algorithm with while but the application is breaking. the code is below. how to do this without while loop in c# ?

   `private void toolStripButton2_Click(object sender, EventArgs e)
    {
        TcpClient client = new TcpClient();
        ModbusIpMaster master = ModbusIpMaster.CreateIp(client);
        client.Connect("192.168.0.1", 502);

        ushort[] offsets = new ushort[4];
        for (int j = 0; j < a; j++)
        {
            string[] numbers = Regex.Split(coordinateLabel[j].Text, @"\D+");
            int index = 0;
            foreach (string value in numbers)
            {
                if (!string.IsNullOrEmpty(value))
                {
                    int i = int.Parse(value);
                    offsets[index] = (ushort)(i);
                    index++;
                }
            }
            master.WriteSingleRegister(0, offsets[1]);
            master.WriteSingleRegister(1, offsets[2]);
            master.WriteSingleRegister(2, (ushort)(variable_Z[j]));
            while (true)
            {
                offsets = master.ReadHoldingRegisters(0, 4);
                if (offsets[3] == 1)
                {
                    master.WriteSingleRegister(0, 0);
                    master.WriteSingleRegister(1, 0);
                    master.WriteSingleRegister(2, 0);
                    while (true)
                    {
                        offsets = master.ReadHoldingRegisters(0, 4);
                        if (offsets[3] == 0)
                        {
                            break;
                        }
                    }
                    break;
                }
            }
        }
        client.Close();
    }`

Upvotes: 2

Views: 5620

Answers (1)

X.Otano
X.Otano

Reputation: 2169

Some advices:

  • Move the implementation inside click event handler to another method/class (read something about SOLID principles)
  • Make the method that checks for the PLC asyncronous , so you dont block the main thread and you don't get that error you are telling about.

--

public class PlcChecker
{
   TcpClient client {get;set;}
   ModbusIpMaster master{get;set;}

   public PlcChecker()
   {
      client= new TcpClient();
    }

    public async Task Connect(string ip,int port)
    {
      ModbusIpMaster master = ModbusIpMaster.CreateIp(client);
      client.Connect(ip,port);
    }

     public async Task<bool> Check()
     {
       //your code to call master and while(true)
     }

}

 public class YourWinForm
{
    public PlcChecker plcChekcer { get; private set; }

    private async void toolStripButton2_Click(object sender, EventArgs e)
    {
        plcChekcer = new PlcChecker();
        await plcChekcer.Connect("192.168.0.1", 502);
        bool result = await plcChekcer.Check();
        if(result)
        {
             //do something   
        }

    }
}

Upvotes: 2

Related Questions