等待窗口的制作 想在用户进行某些比较耗时的操作时,弹出个等待窗体,不能让用户再进行其他的操作,最好是能带GIF动画或者是进度条的一个等待窗体,我知道是用多线程,但是对其不熟悉,最好能有代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我现在的方法是:private void ThreadStart() { th = new Thread(new ThreadStart(ShowLoading)); th.Start(); th.IsBackground = false; } private void ThreadStop() { if (th.IsAlive) { th.Abort(); } } private void ShowLoading() { LoadTip Loadfrm = new LoadTip(); Loadfrm.Msg = "正在加载数据选择窗口……"; Loadfrm.ShowDialog(); Loadfrm.Refresh(); }调用:This.ThreadStart();//费时的操作This.ThreadStop();但是有时候控件会出现红叉叉、有时候会弹出线程正在被中止的对话框 呃..大概说一下:一般准备新建一个线程用于后台处理.做一个提示等待的form,然后将它作为参数传给新的线程接着启动线程开始干活.同时主线程弹出form提示等待.这样用户就无法操作主窗体了.待后台线程干完活,给前台的form发个消息关掉它就可以咯.如果你想在弹出的form上显示进度之类的.只要设计好后台线程和form的通信就可以了.如果和我一样是懒人,直接摆个gif或将进度条的style设置为Marquee摆在上面效果也不错:) 最简单的办法是显示一个你自己设计的等待窗体,但不能GIF动画或者是进度条: FrmTimeWait frmwait = new FrmTimeWait("请稍等,数据正在导出Excel...."); frmwait.Show(); frmwait.Refresh();////该部分放入你要执行的代码// frmwait.Close(); 应该不能重新启动一个线程专门来显示动画图片的进度条,因为主线程进行的时候会把界面阻塞,这样动画图片就不会显示或者就是个红叉叉。我也遇到这个的问题,我使把要加入等待的地方,比如说编译,和显示动画图片都放在一个子线程里面,这样就可以了 //添加一个子线程 Thread trd = null; trd = new Thread(new ThreadStart(ThreadTask)); trd.IsBackground = true; trd.Start();/// <summary>/// 编译和打开进度条/// </summary>public void ThreadTask(){ WorkbenchWindow.WaitBar.BeginInvoke(new EventHandler(WorkbenchWindow.ShowPic));//显示进度条 try { Build(null, null);//编译 这边使处理比较费时的东西,所以要加上个等待 } catch { } finally { WorkbenchWindow.WaitBar.BeginInvoke(new EventHandle(WorkbenchWindow.ClosePic)); //关闭进度条 } } 直接拖个Image,上面放gif图片,Enable=false 。代码的第一句写Image1.Enable=true;最后一句写Image1.Enable=false; //frmwait.cs////编译:csc frmwait.csusing 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(); } }}写了个例子,LZ 拿去看下 标准的多线程 + ProgressBar 多线程 控制ProgressBar显示,异步调用,[转载]两个Form,一个Form中处理数据,另一个是ShowDialog()出来的上面就一个progressBar用来显示进度!! 新建一个Form2;放一个label显示百分比,放一个progressbar显示进度.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(); } } } }} 这只是一个假的进度条,楼主可以在form1在开一个进程,象form2传递progressbar的value. to jianxuanlu :WorkbenchWindow要包含哪个类呀.没用过 用线程打开模式窗体,在执行完操作之后,掉用该窗体方法将其关闭。Thread t=new Thread(new ThreadStart(打开窗体方法));t.isbackground=true;t.start();OK,在执行完成之后,关掉窗体就行。 完美等待窗体参考我下载空间,完美的等待窗体,一行代码搞定。哈哈哈自我吹嘘一下,非常简单││博客空间:http://blog.csdn.net/lovingkiss││资源下载:http://download.csdn.net/user/lovingkiss││Email:loving-kiss@163.com [MSN也是这个]││联系方式:Q66840199 项目、聊天、咨询、探讨、统统收费,呵呵......╚---------------------------------------------------------------------------------------------------------ō*︻$▅▆▇◤ 实际上,等待窗体制作的难度,在于:1、如何使用ShowDialog()不影响主线程的动作;2、如何随时切换主线程、进度线程的动作、进度;3、如何用最少的线程和代码来实现它;。所以这就是我开发这套组件的初衷 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); } } Notepad++写的,在.net2.0下编译.03是1.0当然一堆问题. asp.net web application 创建在自定义文件夹中 Windows服务中如何弹出消息框 C#如何实现类似QQ背景图片的边缘模糊的效果 关于存储过程(输出参数) 输入字母串转换成相对应的图片 请问个C#类内部访问权限问题 求大师解救 请问VS2012跑起来的WEB API如何在其他机器访问 c#里面如何定义一个带单个双引号的字符串,注意是单个双引号,而且在字符串尾部 求救:DataGrid中的DateTime字段无法赋给DateTimePicker.Value??? VS里怎么能从"转到定义" 返回? vs2005中,项目属性窗口突然就无法打开了,是怎么回事,该如何解决?
private void ThreadStart()
{
th = new Thread(new ThreadStart(ShowLoading));
th.Start();
th.IsBackground = false;
}
private void ThreadStop()
{
if (th.IsAlive)
{
th.Abort();
}
}
private void ShowLoading()
{
LoadTip Loadfrm = new LoadTip();
Loadfrm.Msg = "正在加载数据选择窗口……";
Loadfrm.ShowDialog();
Loadfrm.Refresh();
}
调用:
This.ThreadStart();
//费时的操作
This.ThreadStop();
但是有时候控件会出现红叉叉、有时候会弹出线程正在被中止的对话框
大概说一下:一般准备新建一个线程用于后台处理.
做一个提示等待的form,
然后将它作为参数传给新的线程
接着启动线程开始干活.同时主线程弹出form提示等待.
这样用户就无法操作主窗体了.待后台线程干完活,给前台的form发个消息关掉它就可以咯.如果你想在弹出的form上显示进度之类的.只要设计好后台线程和form的通信就可以了.
如果和我一样是懒人,直接摆个gif或将进度条的style设置为Marquee摆在上面效果也不错:)
最简单的办法是显示一个你自己设计的等待窗体,但不能GIF动画或者是进度条:
FrmTimeWait frmwait = new FrmTimeWait("请稍等,数据正在导出Excel....");
frmwait.Show();
frmwait.Refresh();
//
//该部分放入你要执行的代码
//
frmwait.Close();
//添加一个子线程
Thread trd = null;
trd = new Thread(new ThreadStart(ThreadTask));
trd.IsBackground = true;
trd.Start();/// <summary>
/// 编译和打开进度条
/// </summary>
public void ThreadTask()
{
WorkbenchWindow.WaitBar.BeginInvoke(new EventHandler(WorkbenchWindow.ShowPic));//显示进度条
try
{
Build(null, null);//编译 这边使处理比较费时的东西,所以要加上个等待
}
catch
{
}
finally
{
WorkbenchWindow.WaitBar.BeginInvoke(new EventHandle(WorkbenchWindow.ClosePic)); //关闭进度条
}
}
代码的第一句写Image1.Enable=true;
最后一句写Image1.Enable=false;
//
//编译:csc frmwait.csusing 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();
}
}
}
写了个例子,LZ 拿去看下
[转载]两个Form,一个Form中处理数据,另一个是ShowDialog()出来的上面就一个progressBar用来显示进度!!
放一个label显示百分比,放一个progressbar显示进度.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();
}
}
}
}
}
楼主可以在form1在开一个进程,象form2传递progressbar的value.
WorkbenchWindow要包含哪个类呀.
没用过
Thread t=new Thread(new ThreadStart(打开窗体方法));
t.isbackground=true;
t.start();
OK,在执行完成之后,关掉窗体就行。
自我吹嘘一下,非常简单
││博客空间:http://blog.csdn.net/lovingkiss
││资源下载:http://download.csdn.net/user/lovingkiss
││Email:loving-kiss@163.com [MSN也是这个]
││联系方式:Q66840199 项目、聊天、咨询、探讨、统统收费,呵呵......
╚---------------------------------------------------------------------------------------------------------ō*︻$▅▆▇◤
1、如何使用ShowDialog()不影响主线程的动作;
2、如何随时切换主线程、进度线程的动作、进度;
3、如何用最少的线程和代码来实现它;
。所以这就是我开发这套组件的初衷
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);
}
}