c# - How to determine that I have to reconnect basing on catched exception -
i'm using 3rd party library exchange data devices modbus protocol. code similar to:
if (modbusmaster == null) modbusmaster = create(); //creating new connection try { modbusmaster.read(); } catch(exception e) { modbusmaster.dispose(); //dispose underlying connection modbusmaster = null; } disposing , creating new neccesary if underlying connection tcp , connection closed. ioexception in 2 cases:
- there no response device, connection active , can reused
- connection forcibly closed , have reconnect.
how can recognize that? need avoid disposing connection when not needed
if aware of exceptions closes underlying connection, can tell program reconnect on such occasion
var lb_needtoreconnect = false; if(modbusmaster == null) modbusmaster = create(); //creating new connection try { modbusmaster.read(); } catch(ioexception ioe) { lb_needtoreconnect = true; // know @ point connection closed } catch(exception e) { //log exception } if(lb_needtoreconnect) { //recreate modbus here } if(modbusmaster == null) { //either work done or reconnect couldnt established }
Comments
Post a Comment