110,577
社区成员
发帖
与我相关
我的任务
分享
LoadTip waitMess = new LoadTip();//等待窗体
bool sFlog = true;
//--显示等待窗体
private void ShowWaitMess()
{
try
{
if (!waitMess.IsDisposed)
{
waitMess.ShowDialog();
}
}
catch (ThreadAbortException Err)
{
MessageBox.Show(Err.Message);
}
}
//--新开辟一个线程调用
public void WaitShow(string msg)
{
try
{
if ( sFlog == true)
{
sFlog = false;
waitMess.Msg = msg;
Thread upgradeThread = null;
upgradeThread = new Thread(new ThreadStart(ShowWaitMess));
upgradeThread.Start();
}
}
catch (ThreadAbortException Err)
{
MessageBox.Show(Err.Message);
}
}
private delegate void CloseFormDelegate();
//--关闭等待窗体
public void WaitClose()
{
//同步到主线程上
if (waitMess.InvokeRequired)
waitMess.Invoke(new CloseFormDelegate(DoCloseJob));
else
DoCloseJob();
}
private void DoCloseJob()
{
try
{
if (!waitMess.IsDisposed)
{
if (waitMess.Created)
{
sFlog = true;
waitMess.Close();
}
}
}
catch (ThreadAbortException Err)
{
MessageBox.Show(Err.Message);
}
}
namespace WindowsApplication2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public delegate void showprogress();
private void Form2_Load(object sender, EventArgs e)
{
this.progressBar1.Maximum = 100;
this.progressBar1.Minimum = 0;
new System.Threading.Thread(new System.Threading.ThreadStart (ShowBar)).Start();
}
public void ShowBar()
{
if (this.InvokeRequired)
this.Invoke(new showprogress(ShowBar));
else
{
for (int i = 0; i < 101; i++)
{
this.label1.Text = i.ToString() + "%";
this.progressBar1.Value = i;
System.Threading.Thread.Sleep(100);
if (i == 100)
this.Close();
}
}
}
}
}
//frmwait.cs
//
//编译:csc frmwait.cs
using System;
using System.Threading;
using System.Windows.Forms;
class TestFrm:Form
{
Button btn = new Button ();
void bgThread (object arg)//后台线程
{
WaitForm wf = (WaitForm) arg;
//todo: 在这里做后台工作,这里用个空循环
int counter = 0;
while (counter < 2000) {
wf.Progress = counter / 2000f;
counter++;
Thread.Sleep (10);
}
MethodInvoker funcClose = wf.Close;
wf.Invoke (funcClose);
}
TestFrm ()
{
Controls.Add (btn);
btn.Text = "开始后台工作";
btn.Click += btn_click;
}
void btn_click (object sender,EventArgs e)
{
WaitForm wf = new WaitForm();
Thread th = new Thread (bgThread);
th.Start(wf);
wf.ShowDialog();
MessageBox.Show ("后台操作完成!");
}
static void Main ()
{
Application.EnableVisualStyles();
Application.Run(new TestFrm());
}
}
class WaitForm:Form
{
ProgressBar pb = new ProgressBar ();
public WaitForm()
{
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Width = 200;
this.Height = 50;
this.StartPosition = FormStartPosition.CenterParent;
this.ControlBox = false;
pb.Dock = DockStyle.Fill;
pb.Maximum = 100;
Controls.Add (pb);
Text = "后台操作中...";
}
public float Progress
{
get{
float ret = 0f;
MethodInvoker func = delegate()
{
ret = pb.Value / (float)pb.Maximum;
};
if (this.InvokeRequired)
Invoke (func);
else
func();
return ret;
}
set
{
MethodInvoker func = delegate()
{
pb.Value = (int)(pb.Maximum * value);
this.Text = string.Format ("后台操作中,已完成{0}%",value*100);
};
if (this.InvokeRequired)
Invoke (func);
else
func();
}
}
}