locked
WebClient DownloadStringAsync FREEZE My UI

    Question

  • Hello, I have a problem with DownloadStringAsync,

    This function may not Freeze the UI if we refer to the MSDN "This method does not block the calling thread"

    i wanted to get the web page html faster asynchronously, but this is not working for me

    CODE:

     

    in A DLL
    Client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Client_DownloadStringCompleted);
    Client.DownloadStringAsync(Uri);
    

     

    Please Help me,

    Thanks..


    Sunday, July 17, 2011 11:56 AM

Answers

  • GlobalProxySelection.GetEmptyWebProxy(); is now an Obsolete class 
    Then the source change to
    wc.Proxy = null => MSDN
    This class has been deprecated. Please use WebRequest.DefaultWebProxy
    instead to access and set the global default proxy. Use 'null' instead of
     GetEmptyWebProxy.
     http://go.microsoft.com/fwlink/?linkid=14202
    • Marked as answer by DotNETCoder31 Tuesday, July 19, 2011 1:49 PM
    Tuesday, July 19, 2011 1:49 PM

All replies

  • It's most likely that something in: Client_DownloadStringCompleted is blocking your thread. Can you post the code from :

    Client_DownloadStringCompleted

    Monday, July 18, 2011 4:45 PM
  • Hello

    Thank you for your Response

    I have deleted All the code in

    Client_DownloadStringCompleted

    But I Have the same problem

    I'm sure that the problem is with DownloadStringAsync Function

    sorry for my English But I'm Still Learnings.

     

     

     

    Monday, July 18, 2011 9:01 PM
  • Okay, let's try something else. If you're sure the problem is with Client.DownloadStringAsync(Uri); then replace Uri with an actual string (e.g. "www.mywebpage.tt").

     

    Monday, July 18, 2011 9:04 PM
  • Yes I Replaced Uri By "www.bing.com" and i Have The Same problem.

     

    Thanks for your Help

     

     

    Monday, July 18, 2011 10:25 PM
  • Okay. I'm not sure what to say, except it must be something else going on at the same time. Can you post the complete code for that method that calls the Client.DownloadStringAsync please. Just that one method. I've used it before and it's never blocked execution for me.
    Monday, July 18, 2011 10:30 PM
  • Yes

     //
    //DLL CODE
     public void GetResult()
        {
           
           
                  wc = new WebClient();
                  wc.DownloadStringCompleted += 
      new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
                  wc.DownloadStringAsync(new Uri(URI));
             
     
          }
    
      private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
    
        There is Nothing I Deleted all the code for testing 
    
           
         OperationCompleteEventArgs E = new OperationCompleteEventArgs();
    
          E.SetResult("TEST");
    
        OnComplete(sender, E); // Event
           
        }
    // GUI PROGRAM CODE :
        public Form()
        {
       
          Library.OnComplete += new OperationCompletedEventHandler(Converter_OnComplete);
          InitializeComponent();
        }
     private void button1_Click(object sender, EventArgs e)
        {
       
         Library.GetResult();
    
        }
    
        void Converter_OnComplete(object sender, OperationCompletedEventArgs e)
        {
          label1.Text = e.Result;
    
        }
    
    
    
    

    Monday, July 18, 2011 10:51 PM
  • Changed the event handler to this. Check your debug console to see if it hits the event. If it does the problem is after that.
     private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
          Console.WriteLine("Hit Event");
    }
    
    

    Monday, July 18, 2011 11:18 PM
  • Okay,

    When I click on the Button, the GUI Freeze for some Second and after the freeze, i saw the "Hit Event"

    Monday, July 18, 2011 11:25 PM
  • Great. I think I see what going on here. Move these two lines to the library constractor:

      wc = new WebClient();
           wc.DownloadStringCompleted += 
     new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);

    Then try it again. If it works, I'll explain my theory on it. Just have to do a little bit of research.

    Monday, July 18, 2011 11:30 PM
  • I'm Sorry But It Doesn't works,
    Monday, July 18, 2011 11:34 PM
  • I think I got it now. Do this:

     Console.WriteLine("Starting WebClient init");
     wc = new WebClient();
     Console.WriteLine("EndingWebClient init");
        wc.DownloadStringCompleted += 
     new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
    
    

    This will tell you if it's the WebClient constructor that taking a while and "freezing" the app. Not sure why it would unless wc is Static. But give it a shot. 

    Monday, July 18, 2011 11:43 PM
  • No there's no Freeze at initialization of WebClient because I Initialize The Library when the Application Start ,

    and Application start normally without any freeze...

    The Freeze is only when i click on The Button and calling

     wc.DownloadStringAsync(new Uri(URI));

     

    Monday, July 18, 2011 11:55 PM
  • I've been trying to replicate it here and I can't seem to:
    private void button1_Click(object sender, EventArgs e)
        {
          WebClient wc = new WebClient();
          wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
          wc.DownloadStringAsync(new Uri("http://www.google.ca"));
    
        }
        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
          Console.WriteLine("DoneEvent"); 
        }
    
    This does not cause any freezing at all. I've tried some sites that return more data to issue if it was an issue with that and it was not. I think you have to review your library class. I'm sure we're missing something.
    Tuesday, July 19, 2011 12:14 AM
  • Hello,

    I Try to do it but it doesn't works, The Application Freeze, and after the Freeze, i Saw "DoneEvent", and I tried to do the same thing but by creating a new project, but I still have the problem I Think
    DownloadStringAsync is the problem.

    Tuesday, July 19, 2011 1:04 PM
  • I'm sorry, I'm not sure what else to try. I've ran it a few different ways here and I didn't have a problem. If you're using .net 4.0 try 3.5 to see if the problem is still there. Otherwise, you'll have to have a look at all threads running at that time to make sure there's nothing that's causing this method to yield to another thread.
    Tuesday, July 19, 2011 1:06 PM
  • I'm using .NET 4 i tried with 3.5 & 3 but i have the same problem. and I Tried even for a new project,

    I Have The Same problem

     

    I'm on Windows 7 FRENCH & Visual Studio 2010 SP1 FRENCH

    Tuesday, July 19, 2011 1:22 PM
  • I Found The Solution and it works is about Automatic Proxy Detection.

     wc.Proxy = GlobalProxySelection.GetEmptyWebProxy();
    

    Thanks You,

     

     

     

     

     


    • Marked as answer by DotNETCoder31 Tuesday, July 19, 2011 1:37 PM
    • Unmarked as answer by DotNETCoder31 Tuesday, July 19, 2011 1:49 PM
    Tuesday, July 19, 2011 1:37 PM
  • GlobalProxySelection.GetEmptyWebProxy(); is now an Obsolete class 
    Then the source change to
    wc.Proxy = null => MSDN
    This class has been deprecated. Please use WebRequest.DefaultWebProxy
    instead to access and set the global default proxy. Use 'null' instead of
     GetEmptyWebProxy.
     http://go.microsoft.com/fwlink/?linkid=14202
    • Marked as answer by DotNETCoder31 Tuesday, July 19, 2011 1:49 PM
    Tuesday, July 19, 2011 1:49 PM