等待窗口的制作

gengxin_914 2008-05-28 04:02:13
想在用户进行某些比较耗时的操作时,弹出个等待窗体,不能让用户再进行其他的操作,最好是能带GIF动画或者是进度条的一个等待窗体,我知道是用多线程,但是对其不熟悉,最好能有代码
...全文
2595 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
westbb_03 2010-05-27
  • 打赏
  • 举报
回复
C#中实现用委托,c++中用一个定时器或线程
ChrisAK 2008-12-27
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 zzyhuian06142 的回复:]
引用 9 楼 ChrisAK 的回复:
C# code//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 (…
[/Quote]Notepad++写的,在.net2.0下编译.03是1.0当然一堆问题.
zzyhuian06142 2008-07-04
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ChrisAK 的回复:]
C# code//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.Progres…
[/Quote]
这个是在什么环境下写的,我在03下做怎么一堆问题
冷月孤峰 2008-06-02
  • 打赏
  • 举报
回复


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);
}
}

lovingkiss 2008-06-02
  • 打赏
  • 举报
回复
实际上,等待窗体制作的难度,在于:
1、如何使用ShowDialog()不影响主线程的动作;
2、如何随时切换主线程、进度线程的动作、进度;
3、如何用最少的线程和代码来实现它;
。。。。

所以这就是我开发这套组件的初衷。。。
lovingkiss 2008-06-02
  • 打赏
  • 举报
回复
完美等待窗体

参考我下载空间,完美的等待窗体,一行代码搞定。。。。。。。。。。哈哈哈
自我吹嘘一下,非常简单


││博客空间:http://blog.csdn.net/lovingkiss
││资源下载:http://download.csdn.net/user/lovingkiss
││Email:loving-kiss@163.com [MSN也是这个]
││联系方式:Q66840199 项目、聊天、咨询、探讨、统统收费,呵呵......
╚---------------------------------------------------------------------------------------------------------ō*︻$▅▆▇◤
Janyue 2008-05-30
  • 打赏
  • 举报
回复
waiting...........
liulcster 2008-05-30
  • 打赏
  • 举报
回复
to jianxuanlu :
WorkbenchWindow要包含哪个类呀.
没用过
baihe_591 2008-05-30
  • 打赏
  • 举报
回复
这只是一个假的进度条,
楼主可以在form1在开一个进程,象form2传递progressbar的value.
baihe_591 2008-05-30
  • 打赏
  • 举报
回复
新建一个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();
}
}
}
}
}
Jack_Senlan 2008-05-30
  • 打赏
  • 举报
回复

帮顶,
voice007 2008-05-30
  • 打赏
  • 举报
回复
mark
破碎的脸 2008-05-30
  • 打赏
  • 举报
回复
用线程打开模式窗体,在执行完操作之后,掉用该窗体方法将其关闭。
Thread t=new Thread(new ThreadStart(打开窗体方法));
t.isbackground=true;
t.start();


OK,在执行完成之后,关掉窗体就行。
amandag 2008-05-29
  • 打赏
  • 举报
回复
标准的多线程 + ProgressBar
ChrisAK 2008-05-28
  • 打赏
  • 举报
回复
//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();
}
}
}

写了个例子,LZ 拿去看下
DengXingJie 2008-05-28
  • 打赏
  • 举报
回复
mohugomohu 2008-05-28
  • 打赏
  • 举报
回复
直接拖个Image,上面放gif图片,Enable=false 。
代码的第一句写Image1.Enable=true;
最后一句写Image1.Enable=false;
Jerry-lu 2008-05-28
  • 打赏
  • 举报
回复
应该不能重新启动一个线程专门来显示动画图片的进度条,因为主线程进行的时候会把界面阻塞,这样动画图片就不会显示或者就是个红叉叉。我也遇到这个的问题,我使把要加入等待的地方,比如说编译,和显示动画图片都放在一个子线程里面,这样就可以了
//添加一个子线程
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)); //关闭进度条
}
}


sren2008 2008-05-28
  • 打赏
  • 举报
回复
滚动条 你可以使用 始终流动式的啊 。。。 就是 从1到 100 之后 判断 到 100了 再返回到 1 ,这种也很流行的。
加载更多回复(4)

110,577

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧