Link to home
Start Free TrialLog in
Avatar of rpm
rpmFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Using Internet Transfer Contol (or Alternative!)

I am using the VB6 Internet Transfer Control Method OpenURL within a project of mine to lookup data from a legacy system via a HTTP based CLI.

This works fine, but sometimes the requests come too close together causing a runtime error because the previous request is still executing.

I have tried:

URL = "http://10.0.0.23/checkbl.asp?ticket=" & Ticket & "&pn=" & FNumber
Do While Internet.StillExecuting
  DoEvents
Loop
OPut = Internet.OpenURL(URL)

But the StillExecuting property often never returns to false, causing the code to get stuck.

How can I fix this, or is there a better approad altogether?
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

But OpenUrl method is not async, it doesn't return control to your program until finished. Are you using it in a timer event?
Avatar of rpm

ASKER

The events are triggered by user input on a form and can be quite close together (1 - 2 seconds apart.) If OpenURL is not async, how come I am getting errors that the previous request is still executing when I call OpenURL.

I am not using Execute at all in this code.
But you are answering yourself:
"The events are triggered by user input on a form and can be quite close together "

I think user never waits until full cycle is completed so he is calling openurl over and over again.
If you are using a button to call the code, try disabling it until openurl method returns.
There is a free ftp component you could use to transfer files instead of internet transfer control:
http://www.freevbcode.com/ShowCode.Asp?ID=1593

and

http://www.chilkat.com/ 
Avatar of JohnBPrice
JohnBPrice

Do this:

While Inet1.StillExecuting
    MsgBox "Not done yet"
    Exit Sub
Wend

And it will work.

Here is my opinion of what is happening.  It's because VB is not multi-threaded, but it is reentrant, e.g. you can interupt the process to do something else.  When the user clicks too fast on the control, Windows and VB interrupts the process (e.g. inet control) to send the new the input.  VB runs your loop:

Do While Internet.StillExecuting
 DoEvents
Loop

until the cows come home because the first inet is never allowed to finish until the second request is done.  DoEvents allows other inputs to be processed, which only makes matters worse.  Likewise you can't sleep because you only have one thread, and you will just sleep forever.  What you need to do is respond and get out of the second request, which is why the exit sub works.

If you have to queue up user requests, make subsequent calls (when inet is stillexecuting), put the request in a queue and then exit.  After each request, check the queue to see if new requests have been added.

John



Another option is to prevent second requests in the first place, e.g. if you have Button1 which initiates the request, do

button1.enabled = false

URL = "http://10.0.0.23/checkbl.asp?ticket=" & Ticket & "&pn=" & FNumber
OPut = Internet.OpenURL(URL)

button1.enabled = true
Avatar of rpm

ASKER

Following from the advice given, I have tried to change my code accordingly.

Firstly I have created two Subs:

Private Sub PreInternet()

  Do While IntBusy.Caption = "1"
    DoEvents
  Loop
  IntBusy.Caption = "1"

End Sub

Private Sub PostInternet()

  IntBusy.Caption = "0"

End Sub

I have also blocked the triggering events whilst executing the code:

Me.MousePointer = vbHourglass
    If PN1.Text = "Home" Then
      HomePhoneC.Enabled = False
      HomePhoneN.Enabled = False
    End If
    If PN1.Text = "Work" Then
      WorkPhoneC.Enabled = False
      WorkPhoneN.Enabled = False
    End If
    If PN1.Text = "Mobile" Then
      MobilePhoneC.Enabled = False
      MobilePhoneN.Enabled = False
    End If
    If PN1.Text = "Alternate" Then
      AltPhoneC.Enabled = False
      AltPhoneN.Enabled = False
    End If
    If PN1.Text = "Fax" Then
      FaxC.Enabled = False
      FaxN.Enabled = False
    End If
    PN1.Enabled = False
    PN2.Enabled = False
    URL = "http://10.0.0.23/checkbl.asp?ticket=" & Ticket & "&pn=" & FNumber
    PreInternet
    OPut = Internet.OpenURL(URL)
    PostInternet
    HomePhoneC.Enabled = True
    HomePhoneN.Enabled = True
    WorkPhoneC.Enabled = True
    WorkPhoneN.Enabled = True
    MobilePhoneC.Enabled = True
    MobilePhoneN.Enabled = True
    AltPhoneC.Enabled = True
    AltPhoneN.Enabled = True
    FaxC.Enabled = True
    FaxN.Enabled = True
    PN1.Enabled = True
    PN2.Enabled = True
    Me.MousePointer = vbNormal

However I am still getting the code stuck in the PreInternet Sub.

What am I doing wrong now?
ASKER CERTIFIED SOLUTION
Avatar of JohnBPrice
JohnBPrice

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial