c# – WebClient.DownloadDataAsync冻结了我的UI
发布时间:2020-12-15 18:33:19 所属栏目:百科 来源:网络整理
导读:我在我的Form构造函数中,在InitializeComponent之后有以下代码: using (WebClient client = new WebClient()){ client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted); client.DownloadDataAsync("http://
我在我的Form构造函数中,在InitializeComponent之后有以下代码:
using (WebClient client = new WebClient()) { client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted); client.DownloadDataAsync("http://example.com/version.txt"); } 当我启动表单时,UI不会出现,直到引发client_DownloadDataCompleted. 我做错了什么? 谢谢你的时间. 完整代码: 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; } } 解决方法
现在我们已经有了完整的代码,我可以说我绝对没有看到问题 – 不管怎样都没有描述.
在DownloadDataAsync调用之前和之后,以及触发完成的处理程序时,我有一些日志记录指示.如果我通过3G下载大文件,“之前”和“之后”之间会有一个暂停,但是在文件完成下载之前,UI会出现. 我怀疑连接是同步完成的,但实际下载是异步的.当然,这仍然是不幸的 – 并且可能将所有这些都放到一个不同的线程中是要走的路 – 但如果我是对的,至少值得了解. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |