C#想实现下面的功能:
在后台处理一个较复杂的操作的时候出现弹出的等待对话框,避免前台界面无反应,当处理完成后该对话框自动关闭。
要想实现该如何处理?
在后台处理一个较复杂的操作的时候出现弹出的等待对话框,避免前台界面无反应,当处理完成后该对话框自动关闭。
要想实现该如何处理?
解决方案 »
- 请问C#中如何解析下面的XML文件?
- winform修改上传图片大小问题
- C# 如何设置 sqlserver中的选项?比较困难,请高手帮忙一下。谢谢!
- 如何做到只有登录才能浏览某些板块?
- vs2003打包问题,我把我的程序的打包文件依赖项的dll打进包 为什么安装时还提示要.net framwork
- 请高手们来帮忙啊 为什么asp.net写的程序在本地没有问题,可上了服务器按钮就不起作用了啊?
- 怎样用telnet通过IP和端口与设备相连?用C#
- C# 组件开发问题
- 问一个菜问题,别笑话
- 我在vs环境运行任何程序都出现“传递到系统调用的数据区太小”这个错误,而不能运行。请问是怎么回事。
- 怎样将文字转换成可以在word文档中粘贴的图片
- 一个Form1显示在另一个Form2的控件上,怎么做,求高手!!!!
http://hi.baidu.com/cnwhm/blog/item/fd937c34fc4e1a205ab5f5ce.html
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;
} }
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;
http://hi.baidu.com/xdragon/blog/item/b845c650b745f0591138c253.html
或者自己开一个Thread,后台运行。
//写了一个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