Click here to Skip to main content
15,923,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I want to constantly downlaod a string from Webclient using threading, but I can't seem to find a way to do it.

I have a VPN and a label on my form, I want something that whenever I connect to my vpn or disconnect the label updates the text to the ip.

What I have tried:

Do Until i = -1
            i = i + 1
            Try
                Dim client As WebClient = New WebClient()
                Dim reply As String = client.DownloadString("http://tools.feron.it/php/ip.php")
                eLabel2.Text = reply

            Catch ex As Exception
                Dim client As WebClient = New WebClient()
                Dim reply As String = client.DownloadString("http://tools.feron.it/php/ip.php")
                eLabel2.Text = reply

            End Try
        Loop
Posted
Updated 26-Sep-16 20:20pm
v3
Comments
[no name] 21-Sep-16 16:10pm    
Can't find a way to do what? There is nothing to indicate multithreading anywhere in your code. And if there was, you can't update UI controls from a thread.
Daniel Santos 21-Sep-16 18:39pm    
The question is: How can I constantly download a string from a webclient?
__________________________________________________________
CheckForIllegalCrossThreadCalls = False
thread = New System.Threading.Thread(AddressOf downloadip)
thread.Start()

You do NOT do it by "constantly downloading a string". That would be called polling and is rather CPU intensive as well as generating a ton of web traffic and possibly pissing off the owner of the web site you're abusing.

You do this by handling an event that notifies you of changes to IP addresses on any network interface, such as the NetworkChange.NetworkAddressChanged Event (System.Net.NetworkInformation)[^]
 
Share this answer
 
// See https://www.ipify.org/
// the author / owner states it may be used "without limit"

Dim client as New System.Net.WebClient
Dim ip as String = client.DownloadString("https://api.ipify.org")
 
Share this answer
 
Comments
Patrice T 27-Sep-16 3:25am    
Not a reason to abuse !
Matt McKinney 27-Sep-16 14:27pm    
I absolutely agree... there is no reason to poll for external IP like the author believes is needed. Running a background thread/loop for this purpose seems pointless & redundant... unless the author has some really oddball gateway config on the other end of the tunnel.
Perhaps a better way is to trigger off of network events (assuming there is a VPN adapter) followed by a single external IP query.
See https://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkchange.networkaddresschanged(v=vs.110).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900