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

c# – WebClient.DownloadDataAsync冻结了我的UI,第1张

概述我在我的Form构造函数中,在InitializeComponent之后有以下代码: using (WebClient client = new WebClient()){ client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted); cli 我在我的Form构造函数中,在InitializeComponent之后有以下代码:
using (WebClIEnt clIEnt = new WebClIEnt()){    clIEnt.DownloadDataCompleted += new DownloadDataCompletedEventHandler(clIEnt_DownloadDataCompleted);    clIEnt.DownloadDataAsync("http://example.com/version.txt");}

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

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

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

完整代码:

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会出现.

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

总结

以上是内存溢出为你收集整理的c# – WebClient.DownloadDataAsync冻结了我的UI全部内容,希望文章能够帮你解决c# – WebClient.DownloadDataAsync冻结了我的UI所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1242358.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存