C#Winform 等待窗体问题

lcf5w5k 2010-09-16 03:02:02
本人想实现一个功能,就是当主窗体执行Excel操作时,为了掩饰假死现象,打算用一个等待窗体掩饰,在等待窗体中只放一个gif图片。
当主窗体操作执行完毕时,等待窗体自动关闭。
我在网上看到一种方法
WaitForm f = new WaitForm ();
f.Show();
Application.DoEvents();
/*
执行操作
*/
f.Close();
效果是我想要的,不过很致命的是等待窗体的gif动画,没有动,只保持单状态(单桢)。
只能用ShowDialog才能让gif动起来,不过好像要用过线程,本人没接触过线程,网上的代码看上去感觉有残缺(不想要滚动条的那种),请问如何实现,最好有代码。本人不胜感激~~
分不多,已经是全不的了

...全文
815 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
CDG_WJT 2012-03-08
  • 打赏
  • 举报
回复
public partial class FormWaiting : Form
{
#region 委托定义

delegate void DisplayFormDelegate();

delegate void HideFormDelegate();

delegate void UpdateFromWaitingInfoDelegate(string processInfo, int processPersent);

delegate void UpdateSpentTimeInfo();
#endregion

#region 私有字段
System.Windows.Forms.Timer _timer;
static FormWaiting _formWaitingSingleton;
DateTime _beginProcessTime;

#endregion

#region 构造函数
private FormWaiting()
{
InitializeComponent();
_timer = new System.Windows.Forms.Timer();
_timer.Tick += new EventHandler(_timer_Tick);
}


static FormWaiting()
{
_formWaitingSingleton = new FormWaiting();
_formWaitingSingleton.StartPosition = FormStartPosition.CenterScreen;
}

#endregion

#region 公共方法
public void DisplayWaiting()
{
System.Threading.Thread t = new System.Threading.Thread(
new System.Threading.ThreadStart(StartWaiting));
t.IsBackground = true;
t.Start();
}

public void StartWaiting()
{
if (this.InvokeRequired)
{
this.Invoke(new DisplayFormDelegate(Writing));
}
else
{
Writing();
}
}

public void HideWaiting()
{
if (this.InvokeRequired)
{
this.Invoke(new HideFormDelegate(StopWaiting));
}
else
{
this.StopWaiting();
}
}


public static FormWaiting GetFormWaitingInstance()
{
return _formWaitingSingleton;
}

public void ReportProcessState(string processingInfo, int processPersent)
{
if (this.InvokeRequired)
{
this.Invoke(new UpdateFromWaitingInfoDelegate(
this.UpdateFromWaitingInfo), processingInfo, processPersent);
}
else
{
this.UpdateFromWaitingInfo(processingInfo, processPersent);
}
}

#endregion

#region 私有方法

/// <summary>
/// 开始等待
/// </summary>
private void Writing()
{
_timer.Start();

_beginProcessTime = DateTime.Now;

if (this.Visible == false)
{
this.ShowDialog();
}
}

/// <summary>
/// 结束等待
/// </summary>
private void StopWaiting()
{
_timer.Stop();

this.Hide();
}

private void UpdateFromWaitingInfo(string processInfo, int processPersent)
{
this.lblProcessInfo.Text = processInfo;

this.lblProcessedPersent.Text = string.Format("{0}%", processPersent);

this.progressBar1.Value = processPersent;

this.progressBar1.Update();

Application.DoEvents();

}

private void UpdateSpendTime()
{
TimeSpan span = DateTime.Now - this._beginProcessTime;

this.lblSpentTime.Text = string.Format("{0}秒", Convert.ToInt32(span.TotalSeconds));

Application.DoEvents();
}

#endregion

#region 事件
void _timer_Tick(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(new UpdateSpentTimeInfo(UpdateSpendTime));
}
else
{
this.UpdateSpendTime();
}
}

#endregion
}
partial class FormWaiting
{
/// <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()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormWaiting));
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.lblProcessedPersent = new System.Windows.Forms.Label();
this.lblSpentTime = new System.Windows.Forms.Label();
this.lblProcessInfo = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(8, 13);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(61, 61);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 2;
this.pictureBox1.TabStop = false;
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(81, 44);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(427, 12);
this.progressBar1.TabIndex = 3;
//
// lblProcessedPersent
//
this.lblProcessedPersent.AutoSize = true;
this.lblProcessedPersent.Location = new System.Drawing.Point(511, 45);
this.lblProcessedPersent.Name = "lblProcessedPersent";
this.lblProcessedPersent.Size = new System.Drawing.Size(29, 12);
this.lblProcessedPersent.TabIndex = 4;
this.lblProcessedPersent.Text = "进度";
//
// lblSpentTime
//
this.lblSpentTime.AutoSize = true;
this.lblSpentTime.Location = new System.Drawing.Point(514, 69);
this.lblSpentTime.Name = "lblSpentTime";
this.lblSpentTime.Size = new System.Drawing.Size(23, 12);
this.lblSpentTime.TabIndex = 5;
this.lblSpentTime.Text = "0秒";
//
// lblProcessInfo
//
this.lblProcessInfo.AutoSize = true;
this.lblProcessInfo.Location = new System.Drawing.Point(79, 16);
this.lblProcessInfo.Name = "lblProcessInfo";
this.lblProcessInfo.Size = new System.Drawing.Size(113, 12);
this.lblProcessInfo.TabIndex = 0;
this.lblProcessInfo.Text = "正在处理,请等待...";
//
// FormWaiting
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.PaleTurquoise;
this.ClientSize = new System.Drawing.Size(552, 88);
this.ControlBox = false;
this.Controls.Add(this.lblSpentTime);
this.Controls.Add(this.lblProcessedPersent);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.lblProcessInfo);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "FormWaiting";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.TopMost = true;
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Label lblProcessedPersent;
private System.Windows.Forms.Label lblSpentTime;
private System.Windows.Forms.Label lblProcessInfo;
}
JamesXin2011 2011-12-21
  • 打赏
  • 举报
回复
我参照网上指导做了一个。不知道是不是你想要的.也不知道有没有更好的方案。

namespace devDemo
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
frmLoading loading = new frmLoading();
public delegate void delloading();

public delegate void BindedData(List<Product> ls);

private void FormMain_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 关闭loading
/// </summary>
/// <param name="ar"></param>
public void CloseLoading(IAsyncResult ar)
{
this.Invoke(new delloading(() => { loading.Close(); }));
}


private void button1_Click(object sender, EventArgs e)
{
new Action(ReadData).BeginInvoke(new AsyncCallback(CloseLoading), null);
loading.ShowDialog();//显示loading
}

/// <summary>
/// 读取数据
/// </summary>
public void ReadData()
{
List<Product> productList = new List<Product>();
for (int i = 0; i < 15; i++)
{
productList.Add(new Product
{
ProductID = i + 1,
Count = new Random().Next(i * 10, 3000 / (i + 1)),
Pice = System.Math.Round(new Random().NextDouble() * (i + 1) * 100, 4),
Uint = "只",
ProductName = string.Format("产品{0}", i)
});
Thread.Sleep(200);
}

this.Invoke(new BindedData((pls) => { this.protuctBindingSource.DataSource = pls; }),productList);
}
}
}

yangyhq 2011-06-05
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xiaodru 的回复:]
用ShowDialog()可以啊 为什么不用呢
[/Quote]
ShowDialog()是需要返回值的,在取得返回值前,不会其执行下面的语句!
hsliuyl 2011-04-06
  • 打赏
  • 举报
回复
没看懂!这样如何控制进度条根据方法的执行进行度的增加
lcf5w5k 2010-09-17
  • 打赏
  • 举报
回复
多谢高人,待我回去,研究
kj289907795 2010-09-17
  • 打赏
  • 举报
回复
真是好东西啊 。。。。
lcf5w5k 2010-09-17
  • 打赏
  • 举报
回复
请问这个using SysJanitor;
是什么
我在添加引用时也没发现。
这个Janitor.value = 0;
和那个引用是否有关联
baifucn 2010-09-17
  • 打赏
  • 举报
回复
好东西。。。。。。
qqiuzaihui 2010-09-17
  • 打赏
  • 举报
回复
通知子窗体:
//======================================================================
//
// Copyright : SHANGHAI HOMEN INTELLIGENT SYSTEM Co.,LTD.
// All rights reserved
//
// Formname : frmNotify
// Description : 信息通知窗体
//
// created by 枫中玫瑰 at 2010-9-6 20:10:00
//
//======================================================================
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using SysJanitor;
using System.Threading;

namespace FundusSustainLayer
{
public partial class frmNotify : Form
{
public static int value;
public static string notify;

private Thread tNotifyProgress; // 进度条通知线程

private static frmNotify notify;
/// <summary>
/// frmNotify窗体的单例访问器
/// </summary>
public static frmNotify Notify
{
get
{
if (notify == null)
notify = new frmNotify();
return frmNotify.notify;
}
}
private bool bCanClose = false;
/// <summary>
/// 指示当前窗体是否允许被关闭
/// </summary>
public bool BCanClose
{
get { return bCanClose; }
set { bCanClose = value; }
}

private frmNotify()
{
InitializeComponent();
}


/// <summary>
/// 创建通知线程
/// </summary>
public void CreateNotifyThread()
{
Janitor.value = 0;
frmNotify.Notify.SetPBEmpty();


tNotifyProgress = new Thread(NotifyCurrentProgress);
tNotifyProgress.Name = "NotifyProgress";
tNotifyProgress.IsBackground = true;
tNotifyProgress.Start();
}
/// <summary>
/// 通知当前进度
/// </summary>
private void NotifyCurrentProgress()
{
while (true)
{
frmNotify.Notify.SetProgreccBar(frmNotify.value, frmNotify.notify);
System.Threading.Thread.Sleep(50);

if (frmNotify.Notify.BCanClose) return;
}
}
/// <summary>
/// 显示进度条的信息
/// </summary>
/// <param name="value">进度条的当前值</param>
/// <param name="strInfor">当前提示信息</param>
public void SetProgreccBar(int value, string strInfor)
{
if (this.InvokeRequired)
{
try
{
this.Invoke(new Action<int, string>(SetProgreccBar), new object[] { value, strInfor });
}
catch { }
}
else
{
this.labInfor.Text = strInfor;
for (int i = this.pbInfor.Value; i < value % 100; i++)
{
this.pbInfor.Value = i;
this.pbInfor.Refresh();
}

if (value == 100)
{
this.labInfor.Text = "初始化完成!";
this.pbInfor.Value = this.pbInfor.Maximum;
this.Close();
this.bCanClose = true;
}
}
}
}
}


启动窗体:
frmNotify.Notify.BCanClose = false;
frmNotify.Notify.CreateNotifyThread();

通知窗体当前进度:
frmNotify.value = 50;
frmNotify.notify = "test";
frmNotify.Notify.SetProgreccBar(frmNotify.value, frmNotify.notify);

关闭窗体:
frmNotify.Notify.BCanClose = true;
lcf5w5k 2010-09-17
  • 打赏
  • 举报
回复
我就是用ShowDialog(),但不知道当主窗体操作完成后,如何将他关闭
在主窗体对其用Close()是没用的
xiaodru 2010-09-17
  • 打赏
  • 举报
回复
用ShowDialog()可以啊 为什么不用呢
lcf5w5k 2010-09-17
  • 打赏
  • 举报
回复
自己顶,不知道是嫌我分少呀,还是这里已经没高手了呢
lcf5w5k 2010-09-16
  • 打赏
  • 举报
回复
本人开始就没打算用进度条,加之gif动画是自己做的,不想不用,所以只想用等待窗体的形式
linwanhai 2010-09-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xuzysun 的回复:]
[/Quote]
委托
xuzysun 2010-09-16
  • 打赏
  • 举报
回复
用进度条!
tyang258 2010-09-16
  • 打赏
  • 举报
回复
为什么不用个进度条,而要用个窗体
pan973 2010-09-16
  • 打赏
  • 举报
回复
这个也是我需要的。。我操作excel界面也假死。。不知道怎么搞
benyouyong 2010-09-16
  • 打赏
  • 举报
回复
用ShowDialog吧.
这样还有个好处就是这个窗体在的时候,后面的窗体不会获得焦点.

110,577

社区成员

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

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

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