WebClient.DownloadDataAsync freezes my interface

In my form constructor, after InitializeComponent, I have the following code:

using (WebClient client = new WebClient())
{
    client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
    client.DownloadDataAsync("http://example.com/version.txt");
}

When I run my form, the user interface does not appear until the client_DownloadDataCompleted is raised. The client_DownloadDataCompleted method is empty, so there are no problems.

What am I doing wrong? How to do this without freezing the user interface?

Thank you for your time. Regards.

FULL CODE:

Program.cs

using System;
using System.Windows.Forms;

namespace Lala
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Form1.cs

using System;
using System.Net;
using System.Windows.Forms;

namespace Lala
{
    public partial class Form1 : Form
    {
        WebClient client = new WebClient();

        public Form1()
        {
            client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
            client.DownloadDataAsync(new Uri("http://www.google.com"));
            InitializeComponent();
        }

        void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            textBox1.Text += "A";
        }
    }

    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(12, 12);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(12, 41);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(468, 213);
            this.textBox1.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(492, 266);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
    }
}
+5
source share
16 answers

Now that we have the full code, I can say that I definitely do not see the problem - not quite as described, anyway.

, DownloadDataAsync, . 3G, "" "", , .

, , . , , , , - , , , .

+4

. : http://social.msdn.microsoft.com/Forums/en-US/a00dba00-5432-450b-9904-9d343c11888d/webclient-downloadstringasync-freeze-my-ui?forum=ncl

, , - - . :

WebClient webClient = new WebClient();
webClient.Proxy = null;
... Do whatever else ...
+4

, . this .

+1

UNDELETED:. , , , .

, , , webclient.

+1

, WebClient, .

using Dispose . ( , .

, , , .

+1

, -, , , , , .

OnLoad, DesignMode, VS.

+1

DownloadDataAsync vs. DownloadData , UI:

DownloadDataAsync , DownloadDataCompletedEvent , , .

, Jon Skeet - , DNS , HTTP- , DownloadDataAsync .

DNS ?

+1

WPF VS2010,.NET 4.

, WebClient.DownloadDataCompleted ..

, , @Dan: . 1%, , 100%. ( Debug.WriteLn ). .

0% 100%, . , .

+1

:

client.Proxy = GlobalProxySelection.GetEmptyProxy();
+1

, .

Main (Args []) a b, :

    int a, b;
    ThreadPool.GetMaxThreads(out a, out b);

.NET 3.5 VS2008. , , . . :

  • (. ). a = 250 b = 1000
  • VS "Clean" ().
  • /,
  • IE. , IE-/​​
  • Firewall? AntiVirus?
0

.

WebClient, , , . Dispose()

0

using() Dispose() WebClient, . Dispose, , .

using() WebClient DownloadDataCompleted.

0

. , .

, .

, - , .

.NET/Visual Studio ?

0

Ummm....

- ?

- ?

, ZoneAlarm?

0

, ( Visual Studio) .

exe .

0

VS2015. , , , , , , , , . , , progresschanged. , , , , , , .

0

All Articles