首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# wimform 兑现等待窗口

2012-06-25 
C# wimform 实现等待窗口C#想实现下面的功能:在后台处理一个较复杂的操作的时候出现弹出的等待对话框,避免

C# wimform 实现等待窗口
C#想实现下面的功能:  
在后台处理一个较复杂的操作的时候出现弹出的等待对话框,避免前台界面无反应,当处理完成后该对话框自动关闭。  
要想实现该如何处理?

[解决办法]

使用BackGroundWorker控件
[解决办法]

C# code
//主窗体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);            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;        }    }
[解决办法]
C# code
//弹出的提示窗体 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();        }    }//弹出窗体的designer.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; 


[解决办法]

C# code
  //写了一个backgroundWorker例子  //主界面拖一个backgroundWorker组件  public partial class Form1 : Form  {    public Form1()    {      InitializeComponent();       this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);      this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);    }    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)    {      WaitMethod();    }    private void WaitMethod()    {      //模拟等待操作      Thread.Sleep(5000);      //这里不能使用界面控件      //如果要使用,要委托和this.Invoke()    }    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)    {      if (_WaitForm != null && !_WaitForm.IsDisposed)      {        _WaitForm.Close();      }    }    //开始    ProgressBarFrom _WaitForm = null;    private void button1_Click(object sender, EventArgs e)    {      if (!backgroundWorker1.IsBusy)      {        backgroundWorker1.RunWorkerAsync();      }      _WaitForm = new ProgressBarFrom();      _WaitForm.Show();    }  }   //ProgressBarFrom 窗体只需要拉一个 ProgressBar控件,Style设置为:Marquee 

热点排行