Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

httpwebrequest hangs application

Posted on 2009-05-08
18
Medium Priority
?
310 Views
Last Modified: 2013-11-26
hi,

I have a function func. It is started by thread. that func calls another function nav which accepts a string. it uses httpwebrequest and response to get source of website. My Application works well if i use web browser control. it gets hanged when i use httpwebrequest. I am just creatign req, geting response and assigning the text to webbrowser.document text property...
0
Comment
Question by:Yogesh_Agarwal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 9
18 Comments
 
LVL 1

Expert Comment

by:sfun28
ID: 24335906
Have you tried System.Net.Webclient?
I believe its a wrapper on HttpWebRequest/Response.
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24336034
but that doesnt have useragent, timeout....
0
 
LVL 1

Expert Comment

by:sfun28
ID: 24336154
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:sfun28
ID: 24336171
anyways, it sounds like you're dealing with a thread issue.
have you tried calling nav without creating a new thread.  have you tested that it works in single-thread environment?
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24336280
no i din tried. but it has to be called only from thread.. i do want to call out of thread.. it will collide the logic  of my application..
0
 
LVL 1

Expert Comment

by:sfun28
ID: 24336666
well...doing the wrong sorts of things with threads could certainly cause hanging - like if you had a deadlock scenario.  It sounds like you need more testing/debugging - its hard to say what's wrong with the information you've provided, it could really be anything.  I would start with a single thread and step through your code in the debugger.  Does it still hang?  If not, then something you're doing with threading is messing it up.  Isolate the problem down to the most basic case possible and work up from there.
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24336814
there is no dead locks.. it works fine.. only during request and respnses it gets hang..  i am really confused that wat cud eb the problem..
0
 
LVL 1

Expert Comment

by:sfun28
ID: 24342124
can you post code?
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24344973

dim t1 as new thread(addressof web_thread)
'Few Conditions
t1.start
 
sub web_thread()
 
 
Dim s As String = navigate(TextBox1.Text.ToString)
            If s Is Nothing Then
                Button1.PerformClick()
                Exit Sub
            Else
            
                WebBrowser1.DocumentText = s.ToString
         
            End If
 
end sub
 
Public Function Navigate(byval str as string) as String
 Try
 
            If pse = False Then
                Dim wreq As HttpWebRequest
                Dim wres As HttpWebResponse
                wreq = HttpWebRequest.Create(str)
                wreq.AllowAutoRedirect = True
                wres = wreq.GetResponse
                Dim sr As StreamReader
                sr = New StreamReader(wres.GetResponseStream)
                str = sr.ReadToEnd
                str = replacetags(str)
                Return str
 
            End If
 
            Return Nothing
 
        Catch ex As Exception
 
            Return Nothing
        End Try
 
End Function

Open in new window

0
 
LVL 1

Expert Comment

by:sfun28
ID: 24349542
This worked fine for me (see code snippet below).  What is replacetags(str) doing?  is there some kind of recursion or other infinite loop?  have you walked this through the debugger?  what step does it get stuck on?
 Dim t1 As New Thread(AddressOf Navigate)
 
    Public Function Navigate() As String
        Try
            Dim str As String = "http://www.yahoo.com"
 
            Dim wreq As HttpWebRequest
            Dim wres As HttpWebResponse
            wreq = HttpWebRequest.Create(Str)
            wreq.AllowAutoRedirect = True
            wres = wreq.GetResponse
            Dim sr As StreamReader
            sr = New StreamReader(wres.GetResponseStream)
            Str = sr.ReadToEnd
            Return str
 
        Catch ex As Exception
 
            Return Nothing
        End Try
 
    End Function

Open in new window

0
 

Author Comment

by:Yogesh_Agarwal
ID: 24349674
i don want to call navigate function through another thread.. Replace tags will remover certain tags in html.. and will return.. it s just one line of statement.. No problem in that.. HOw can i find that when UI Hangs through debugger?
0
 
LVL 1

Accepted Solution

by:
sfun28 earned 2000 total points
ID: 24349719
You are calling navigate through a separate thread already.  I just cut out the web_thread() sub to isolate the web code.  web_thread calls Navigate on the same thread, the new one you created.  for debugging, just set a breakpoint at:
Dim s As String = navigate(TextBox1.Text.ToString)
please do this right now...just set a breakpoint and step through every line of code and tell me what happens.
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24352090
i did.. wat happens there is,

Goes to function, Executes all line one by one. it stops at get response. UI comes. at that time UI is responsive.. after getting request ui goes and again execution starts line by line.. but wait.. When it comes second time, UI doesnt Shows up.. so it means that at second round UI Get hanged up.. Second time that web_thread functions is raised by Events(Button1.PerformClick()).. so now problem is that raising that function by events right?
0
 
LVL 1

Expert Comment

by:sfun28
ID: 24354395
I'm having trouble understanding the flow of control here.  Button1.PerformClick() is only executed if navigate(TextBox1.Text.ToString) returns nothing.  so what do you mean by "second time that web_thread functions is raised by events(button1.performclick())?

What exactly is calling web_thread?  The code snippet you posted doesn't show that. I see
dim t1 as new thread(addressof web_thread)
which looks like a public instance varible, but how are you actually starting that thread? from where?

0
 

Author Comment

by:Yogesh_Agarwal
ID: 24355529
i am starting that thread when form load event fires.. then after completion of getting response and setting as document text, i am doing few manipulation in document Completed Even of Web browser.. then again i am firing botton1.performclick()     (webthread_1 handles that)...
0
 
LVL 1

Expert Comment

by:sfun28
ID: 24356589
I'm really having a hard time understanding you.  What do you mean when you say " i am doing few manipulation in document Completed Even of Web browser."

so you're telling me that you fire button1.performclick(), which fires web_thread (which itself would fire button1.performclick() if the string is empty?)

i'm still having trouble following your flow.  this sounds unnecessarily complex.  I'm sorry but I cannot afford any more time to help you debug this.  I strongly suggest you take a step back and look at your design.  It seems very complicated.
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24356629
i know that i fal to make u understand... how over i managed to solve me problem.. thanks a lot for spending ur precious time.. u helped me a lot...
0
 

Author Closing Comment

by:Yogesh_Agarwal
ID: 31579428
it tought me a good lesson..
0

Featured Post

Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question