当变量来自plc时,我希望程序等待。我尝试了一段时间的算法,但应用程序坏了。代码如下。如何在c#中没有while循环的情况下做到这一点?
Network error : Software caused connection abort
答案 0 :(得分:1)
一些建议:
-
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
}
}
}