[2005] problem with timer tick event in combination with winsock
Hi,
I use a winsock control to connect to a device on the network. If the connection gets lost, I want to start a timer that tries to reopen the connection every x-seconds (for example, try to reconnect every 30 seconds).
If I use a timer in a test project it works without a problem (timer1.enabled = true & timer1.interval = 30000). Every 30sec the timer1 tick event is trigerred (displaying a messagebox as an indication).
However, if I use the winsock control I enable the timer in the winsock1_HandleError event, it gets enabled, but the timer never gets into the tick event.
This is my code...
under btnStartConnection:
Code:
call make_connection
private sub make_connection
Code:
If Winsock1.GetState <> Winsock_Control.WinsockStates.Closed Then
winsock1.close 'close connection if it's not closed
make_connection 'start this sub again to connect
else
winsock1.connect("192.168.6.131",3317)
end if
private sub winsock1_HandleError(...)
Code:
timer1.enabled = true 'interval is set at 30000 in the properties
messagebox.show("Timer enabled") 'for testing purpose, so i can see it's enabled
private sub timer1_tick(...)
Code:
messagebox.show("Tick event") 'for testing purpose, so i can i got to the tick event
call make_connection
Somethimes it works when I remove the messageboxes for testing. Does anybody know what the problem can be? Or if you have a better way to set up the connection and try to reconnect after an error, you'r welcome to share it.
EDIT:
I got around this problem by starting the timer when I load my project and using a global variable (var1 as byte, because datatype byte only uses 1 byte, as integer uses 4) indicating if I tried to make a connection or not. If not, don't do anything in the tick event, if I did, try to reconnect in the tick event if the connection is lost.
private sub make_connection()
Code:
private sub make_connection()
var1 = 1 '1 because we chose to make a connection
If Winsock1.GetState <> Winsock_Control.WinsockStates.Closed then
winsock1.close
call make_connection
else
winsock1.connect("192.168.6.131",3317)
end if
end sub
private sub frmMain_Load(...)
var1 = 0 'because we didn't ask yet to make a connection
timer1.interval = 30000
timer1.enabled = true
end sub
private sub winsock1_connected(...)
var1 = 2 'connection = ok
end sub
private sub close_connection()
var1 = 3 'because we asked to close the connection - don't try to reconnect in the tick event
end sub
private sub timer1_tick(...)
if var1 = 4 'there's an error with the connection and we did ask to set up one
call make_connection
end if
private sub winsock1_stateChanged(...)
select case true
case ...
case winsock1.getstate = winsock_control.winsockstates.closing
if var1 <> 3 then var1 = 4 'if the connection is closing (for ex. the network cable is disconnected) and we didn't ask to close the connection -> then set var1 indicating there's an error
This systems seems to work fine, but I'm not sure this is the best way because every x-seconds I generate an event that's not always necessary (+ I have to use a global variable). This uses more resources then just enabling the timer when there's a problem and disabling it when everything is ok.