Winform中长时间处理时良好的交互界面呈现_多线程


下面是在此期间的交互窗体,交互窗体在耗时过程中,显示一个时间提示框,用于提示系统还在进一步计算中

下面这个窗体就是弹出的这个提示窗体,即用于提示用户:“我还没挂,正在努力加载中”信息的窗体

  public partial class formTimer : Form
{
        public formTimer()
        {
            InitializeComponent();
        }
        DateTime a = DateTime.Now;
        private void formTimer_Load(object sender, EventArgs e)
        {
            label1.Text = (DateTime.Now - a).Seconds.ToString();
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = (DateTime.Now - a).Seconds.ToString();
        }
    }
为制作这个特殊的交互式窗体,把formTimer.dedigner.cs文件内容一并贴上来(主要是把该窗体的显示样式做一些特殊处理)
 partial class formTimer
    {
        /// <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.components = new System.ComponentModel.Container();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.panel1 = new System.Windows.Forms.Panel();
            this.label1 = new System.Windows.Forms.Label();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // timer1
            // 
            this.timer1.Interval = 1000;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.Color.Transparent;
            this.panel1.Controls.Add(this.label1);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(121, 66);
            this.panel1.TabIndex = 0;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.BackColor = System.Drawing.Color.Transparent;
            this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.label1.Font = new System.Drawing.Font("SimSun", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label1.ForeColor = System.Drawing.Color.Red;
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(0, 40);
            this.label1.TabIndex = 0;
            // 
            // formTimer
            // 
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(121, 66);
            this.Controls.Add(this.panel1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "formTimer";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.TopMost = true;
            this.TransparencyKey = System.Drawing.SystemColors.ControlText;
            this.Load += new System.EventHandler(this.formTimer_Load);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label label1;
    }

 主窗体的调用处:模拟耗时操作
 public partial class DelegateBeginInvoke : Form
    {
        public DelegateBeginInvoke()
        {
            InitializeComponent();
        }

        //定义与方法同签名的委托   
        private delegate string DelegateName(int num, out int ret);
        private delegate void DelegateA();
        private delegate void Delegate赋值(string 值);
        formTimer f = new formTimer();

        //要进行异步的方法   
        private string MethodName(int num, out int ret)
        {
            ret = num;
            System.Threading.Thread.Sleep(10000);//sleep10秒 模拟长时间操作
            return "HelloWorld";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "";
            //实例化委托并初赋值   
            DelegateName dn = new DelegateName(MethodName);
            int i;
            //实例化回调函数,把AsyncCallback看成是Delegate就懂了,实际上AsyncCallback是一种特殊的Delegate;回调函数,顾名思义就是新线程处理完成后的响应方法   
            AsyncCallback acb = new AsyncCallback(CallBackMethod);
            //异步开始    
            //如果参数acb换成null则表示没有回调方法    
            //最后一个参数dn的地方,可以换成任意对象,该对象可以被回调方法从参数中获取出来,写成null也可以。   
            //参数dn相当于该线程的ID,如果有多个异步线程,可以都是null,但是绝对不能一样,不能是同一个object,否则异常    
            IAsyncResult iar = dn.BeginInvoke(1, out i, acb, dn);
            textBox1.Text = Environment.UserName.ToString();
            f = new formTimer();
            f.Show();
        }

        //回调方法(异步完成时,执行的方法,此方法只有IAsyncResult一个参数,但是该参数几乎万能,可以传递Object)   
        private void CallBackMethod(IAsyncResult ar)
        {
            DelegateName dn = (DelegateName)ar.AsyncState;

            int i;

            string r = dn.EndInvoke(out i, ar);//一定要EndInvoke,否则你的下场很惨  
            DelegateA dd = new DelegateA(formClose);
            if (f.InvokeRequired)
                this.Invoke(dd);
            else
                formClose();
            //MessageBox.Show("异步完成了!i的值是" + i.ToString() + ",r的值是" + r);
            Delegate赋值 dd赋值 = new Delegate赋值(赋值);     
            if (label1.InvokeRequired)
            {
                this.Invoke(dd赋值, new object[] { "异步完成了!i的值是" + i.ToString() + ",r的值是" + r });
            }
            else
            {
                textBox1.Text = "异步完成了!i的值是" + i.ToString() + ",r的值是" + r;
            }
        }

        void formClose()
        {
            f.Close();//异步事件完成后,关闭交互窗体
        }
        void 赋值(string aa)
        {
            label1.Text = aa;//用于模拟回发时处理的方法
        }

    }


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值