c# – WebClient.DownloadDataAsync冻结了我的UI

前端之家收集整理的这篇文章主要介绍了c# – WebClient.DownloadDataAsync冻结了我的UI前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的Form构造函数中,在InitializeComponent之后有以下代码
  1. using (WebClient client = new WebClient())
  2. {
  3. client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
  4. client.DownloadDataAsync("http://example.com/version.txt");
  5. }

当我启动表单时,UI不会出现,直到引发client_DownloadDataCompleted.
client_DownloadDataCompleted方法为空,因此没有问题.

我做错了什么?
如何在不冻结UI的情况下做到这一点?

谢谢你的时间.
最好的祝福.

完整代码

Program.cs中

  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace Lala
  5. {
  6. static class Program
  7. {
  8. /// <summary>
  9. /// The main entry point for the application.
  10. /// </summary>
  11. [STAThread]
  12. static void Main()
  13. {
  14. Application.EnableVisualStyles();
  15. Application.SetCompatibleTextRenderingDefault(false);
  16. Application.Run(new Form1());
  17. }
  18. }
  19. }

Form1.cs的

  1. using System;
  2. using System.Net;
  3. using System.Windows.Forms;
  4.  
  5. namespace Lala
  6. {
  7. public partial class Form1 : Form
  8. {
  9. WebClient client = new WebClient();
  10.  
  11. public Form1()
  12. {
  13. client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
  14. client.DownloadDataAsync(new Uri("http://www.google.com"));
  15. InitializeComponent();
  16. }
  17.  
  18. void client_DownloadDataCompleted(object sender,DownloadDataCompletedEventArgs e)
  19. {
  20. textBox1.Text += "A";
  21. }
  22. }
  23.  
  24. partial class Form1
  25. {
  26. /// <summary>
  27. /// required designer variable.
  28. /// </summary>
  29. private System.ComponentModel.IContainer components = null;
  30.  
  31. /// <summary>
  32. /// Clean up any resources being used.
  33. /// </summary>
  34. /// <param name="disposing">true if managed resources should be disposed; otherwise,false.</param>
  35. protected override void Dispose(bool disposing)
  36. {
  37. if (disposing && (components != null))
  38. {
  39. components.Dispose();
  40. }
  41. base.Dispose(disposing);
  42. }
  43.  
  44. #region Windows Form Designer generated code
  45.  
  46. /// <summary>
  47. /// required method for Designer support - do not modify
  48. /// the contents of this method with the code editor.
  49. /// </summary>
  50. private void InitializeComponent()
  51. {
  52. this.button1 = new System.Windows.Forms.Button();
  53. this.textBox1 = new System.Windows.Forms.TextBox();
  54. this.SuspendLayout();
  55. //
  56. // button1
  57. //
  58. this.button1.Location = new System.Drawing.Point(12,12);
  59. this.button1.Name = "button1";
  60. this.button1.Size = new System.Drawing.Size(75,23);
  61. this.button1.TabIndex = 0;
  62. this.button1.Text = "button1";
  63. this.button1.UseVisualStyleBackColor = true;
  64. //
  65. // textBox1
  66. //
  67. this.textBox1.Location = new System.Drawing.Point(12,41);
  68. this.textBox1.Multiline = true;
  69. this.textBox1.Name = "textBox1";
  70. this.textBox1.Size = new System.Drawing.Size(468,213);
  71. this.textBox1.TabIndex = 1;
  72. //
  73. // Form1
  74. //
  75. this.AutoScaleDimensions = new System.Drawing.SizeF(6F,13F);
  76. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  77. this.ClientSize = new System.Drawing.Size(492,266);
  78. this.Controls.Add(this.textBox1);
  79. this.Controls.Add(this.button1);
  80. this.Name = "Form1";
  81. this.Text = "Form1";
  82. this.ResumeLayout(false);
  83. this.PerformLayout();
  84.  
  85. }
  86.  
  87. #endregion
  88.  
  89. private System.Windows.Forms.Button button1;
  90. private System.Windows.Forms.TextBox textBox1;
  91. }
  92. }

解决方法

现在我们已经有了完整的代码,我可以说我绝对没有看到问题 – 不管怎样都没有描述.

在DownloadDataAsync调用之前和之后,以及触发完成的处理程序时,我有一些日志记录指示.如果我通过3G下载大文件,“之前”和“之后”之间会有一个暂停,但是在文件完成下载之前,UI会出现.

我怀疑连接是同步完成的,但实际下载是异步的.当然,这仍然是不幸的 – 并且可能将所有这些都放到一个不同的线程中是要走的路 – 但如果我是对的,至少值得了解.

猜你在找的C#相关文章