Hi,

I'm developping an application that reads emails from a server.

I have set the interval of the timer1 to 1000 *30 (i.e. 30 seconds).

In the timer1_timer () subroutine I called another subroutine which makes a connection
with the server using
Winsock1.RemoteHost "hostname"
Winsock1.RemotePort "hosr port"
Winsock1.Connect

and then reads the emails etc.

then dissconnects from the server, and after 30 seconds or whatever repeats the cycle:
connecting to the server
reading emails
dissconnecting from the server.

The problem is that the timer run through the cycle mentioned above only once,
after 30 seconds it does not do anything.

Please help me.

Thanks a lot, appreciate your help
Aiman

Try the following:

'After your routine add the following
Timer1.Enabled = False
Timer1.Enabled = True
'This should recall your routine every 30 seconds

Also set your timer interval property to 30000 and not 1000*30. You can go up to 60000 with the interval (1 minute)

Hello AndreRet,

Thank you very much for your reply.

Where to put the
Timer1.Enabled = False
Timer1.Enabled = True

Here is the code in short:

Private Sub Form_Load()

Timer1.Interval = 20000 '(20 seconds)
Timer1.Enabled = True

End Sub
__________________________
Private Sub Timer1_Timer()

ConnectAndReadEmails 'This is the subroutine which
connects to the server and reads emails

End Sub
__________________________
Private Sub ConnectAndReadEmails()

Winsock1.RemoteHost = "Here goes the server name"
Winsock1.RemotePort = 110
Winsock1.Connect

'Here goes the code for reading emails

Disconnect 'calls the subroutine for disconnecting from the server

End Sub
___________________________________

Private Sub Dissconnect()
Winsock1.SendData ("quit" + vbCrLf)
Winsock1.Close

End Sub
________________________________


After I run the application, it waits 20 seconds then it
executes the subroutine (ConnectAndReadEmails)
which is in the Timer1_Timer() subroutine.

It connects to the server and reads the emails
Then, goes to the subroutine (Disconnect) and
disconnects from the server.
After 20 seconds (or what remains from it) it suppose
to do the same again, but it doesn't.

After trying several option (and failed), I think that
after connectiing to the server the Timer1 looses its
functionality, because I have made the application
go through the same cycle, but not to connect to
the server, just do something else which does not
involve the Winsock1, and the result was that it
worked well every 20 seconds.

So, what could be wrong in my original application.

Thanks a lot, appreciate your help.
Aiman

Your subroutines looks fine. Add the enabled code in your timer event -

Private Sub Timer1_Timer()

ConnectAndReadEmails 'This is the subroutine which
connects to the server and reads emails

Timer1.Enabled = False 'This will disable the timer for now.
Timer1.Enabled = True ' This will re-enable your timer and re-call your subroutines after 20 seconds. Test your timer with the following event as well to confirm that the problem exists with the timer and not your winsock connection - 

'Clear the ConnectAnd ReadEmails event. Add the following -
Msgbox "Testing after 20 seconds"

Timer1.Enabled = False 
Timer1.Enabled = True
'You should receive the message box after every 20 seconds. If this works fine, you're ok with your code.
End Sub

Hello AndreRet,

Thanks for the reply.

The test with the Msgbox worked, but after returning
everything back it doesn't work.

I forgott to mention in my previous reply that:

If I still connect to the server and read emails every
20 seconds, it worked.
But, if I disconnect from the server and try to connect
again after 20 seconds it doesn't work.

Thanks a lot, appreciate your help
Aiman

It looks like the trouble is your connection after you have closed it. I have attached a zip file with a lot of winsock coding that will help you towards your connection. I have not dealt with winsock for some time, so I am a bit rusty.

I think the "quit" statement you have is also incorrect. It should be .Close. Also try to use a different port number. That also tends to throw things out when trying to re-connect.

Hope the attachment helps.

Hello AndreRet,

Thanks for the file.

I will check it and will inform you about it

By the way, the connection and disconnection are fine
and they do work O.K.

I did another try and added this line:

Private Sub Timer1_Timer()

List1.AddItem "anything" 'This is the new line added

ConnectAndReadEmails 'This is the subroutine which
connects to the server and reads emails

End Sub
________________________________


In the first time after I run the application the string
"anything" is added to the List1, after 20 seconds
it is not added, which means that the Timer1_timer()
subroutine is not executed.


Thanks alot, appreciate your help
Aiman

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.