Results 1 to 10 of 10

Thread: How to Set Time Out for WebClient

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    How to Set Time Out for WebClient

    Say I use

    wb as webclient
    wb.downloadstring(url)

    Say 40 seconds have passed. I want wb to just give up. How to do so?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: How to Set Time Out for WebClient

    As far as I'm aware, you can't. You could call DownloadStringAsync instead and use a Timer to call CancelAsync after a specific period. Otherwise, if you want to actually set a timeout on the operation, you would have to use an HttpWebRequest, which has a Timeout property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: How to Set Time Out for WebClient

    I see. That httpwebrequest seems to be better than webclient isn't it?

    Also is there any chance that webclient will just hang forever?
    Last edited by teguh123; Mar 5th, 2011 at 02:02 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: How to Set Time Out for WebClient

    Quote Originally Posted by teguh123 View Post
    That httpwebrequest seems to be better than webclient isn't it?
    Given that the WebClient uses a WebRequest internally, it's not better. The WebClient is simple to use and is therefore the better choice for simple scenarios. The WebRequest class gives you finer-grained control so it's the better choice for situations where you need finer-grained control.
    Quote Originally Posted by teguh123 View Post
    Also is there any chance that webclient will just hang forever?
    I've never researched that before but I did have a quick look around when I read your question and I saw no specific indication that that is not the case.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: How to Set Time Out for WebClient

    I set my program to just terminate the thread.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: How to Set Time Out for WebClient

    Quote Originally Posted by teguh123 View Post
    I set my program to just terminate the thread.
    That would be bad. As I said, you can use a Timer to cancel an async operation if that's what you need.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: How to Set Time Out for WebClient

    Yea but using async will be too complicated. I suppose, I can do async and then provide a call back function using lamda expression, and then use sleep and then has a loop to check whether it's still busy or not. I'll just have to think about that one. Actually if webclient has a default time out, it'll be fine.

    I think I'll follow your advice and use that async thingy with a special function. So create myDownloadStringFunction

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: How to Set Time Out for WebClient

    Using an async method will not be complicated unless you make it so, which that description does.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: How to Set Time Out for WebClient

    E.g.
    vb.net Code:
    1. Imports System.Net
    2.  
    3. Public Class Form1
    4.  
    5.     Private WithEvents client As WebClient
    6.    
    7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    8.         If Me.client Is Nothing Then
    9.             Me.client = New WebClient
    10.         End If
    11.  
    12.         If Not Me.client.IsBusy Then
    13.             Me.client.DownloadStringAsync(New Uri(Me.TextBox1.Text))
    14.         End If
    15.     End Sub
    16.  
    17.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    18.         If Me.client IsNot Nothing AndAlso Me.client.IsBusy Then
    19.             Me.client.CancelAsync()
    20.         End If
    21.     End Sub
    22.  
    23.     Private Sub client_DownloadStringCompleted(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs) Handles client.DownloadStringCompleted
    24.         If Not e.Cancelled Then
    25.             Me.DisplayDownloadedString(e.Result)
    26.         End If
    27.     End Sub
    28.  
    29.     Private Sub DisplayDownloadedString(ByVal text As String)
    30.         If Me.InvokeRequired Then
    31.             Me.Invoke(New Action(Of String)(AddressOf DisplayDownloadedString), text)
    32.         Else
    33.             MessageBox.Show(text)
    34.         End If
    35.     End Sub
    36.  
    37. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: How to Set Time Out for WebClient

    I know it's not jmcilheney. It's just that I want to use async for synchronous operation (because I already do the multi threading part)

    So I will have to do
    async
    wait till time out or finish
    done

    And if it's timed out, what I will do is terminate the thread anyway. It's okay.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width