以前做过一个这样的计算水电煤气费的winform 虽然很幼稚,不过用到自制ProgressBar+多线程给你看看源码吧using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using BylProgress;namespace _02Fee { public partial class Form1 : Form { public Form1() { InitializeComponent(); radioButton3.Checked = true; unitCount = 3; ReadBinaryFile(); bylProbar.Visible = false; } int c1; int p1=1; private int unitCount; private FileStream file; private Stream stream; private void ReadBinaryFile() { try { stream = new FileStream(@"c:\702fee.ini", FileMode.Open); IFormatter formatter = new BinaryFormatter(); txtLastAllCharge.Text = (string)formatter.Deserialize(stream); } catch (Exception e) {
{
prbState.Visible = true;
prbState.Minimum = 0;
prbState.Step = 1;
prbState.Value = 0;
prbState.Maximum = 100;
for(int i=0;i<100;i++)
{
prbState.PerformStep();
}}
Pffx_hz()和Pffx_gyd()是两个做好的函数,我只要执行这两个函数就可以得到我要的结果,现在是我不知道这两个函数大概要花多少时间,
如果可以,我想大概定义一个时间,让时间慢慢走,然后走到90%的时候,再根据结果到100%完成.可以实现吗?知道的高手请指点或代码,我的Email:[email protected]解决后马上给分,不够可以加分的.
http://blog.csdn.net/jinjazz/archive/2008/05/24/2477088.aspx
<html>
<head>
<title></title>
<script type="text/javascript">
var url = 'http://www.google.co.uk/search?hl=en&q=asp.net&meta='; //the details page you want to display...
</script>
<style>
.loading-indicator {
font-size:8pt;
background-image:url(../images/loading/loading.gif);
background-repeat: no-repeat;
background-position:top left;
padding-left:20px;
height:18px;
text-align:left;
}
#loading{
position:absolute;
left:45%;
top:40%;
border:3px solid #B2D0F7;
background:white url(../images/loading/block-bg.gif) repeat-x;
padding:10px;
font:bold 14px verdana,tahoma,helvetica;
color:#003366;
width:180px;
text-align:center;
}
</style>
<div id="loading">
<div class="loading-indicator">
Page Loading...
</div>
</div>
</head>
<body onload="location.href = url;" style="overflow:hidden;overflow-y:hidden">
</body>
<script>
if(document.layers) {
document.write('<Layer src="' + url + '" visibility="hide"></Layer>');
}
else if(document.all || document.getElementById) {
document.write('<iframe src="' + url + '" style="visibility:hidden;"></iframe>');
}
else {
location.href = url;
}
</script>
</html>
虽然很幼稚,不过用到自制ProgressBar+多线程给你看看源码吧using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using BylProgress;namespace _02Fee
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
radioButton3.Checked = true;
unitCount = 3;
ReadBinaryFile();
bylProbar.Visible = false; }
int c1;
int p1=1;
private int unitCount;
private FileStream file;
private Stream stream;
private void ReadBinaryFile()
{
try
{
stream = new FileStream(@"c:\702fee.ini", FileMode.Open);
IFormatter formatter = new BinaryFormatter();
txtLastAllCharge.Text = (string)formatter.Deserialize(stream);
}
catch (Exception e)
{
statusStrip1.Visible = true;
statusStrip1.Text = e.Message;
txtLastAllCharge.Text = "0";
}
finally
{if(stream!=null)
stream.Close();
//stream.Dispose();
}
} private void WriteBinaryFile()
{
try
{
file = new FileStream(@"c:\702fee.ini", FileMode.Create);
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(file, txtNowAllCharge.Text);
}
finally
{
file.Close();
//file.Dispose();
} } private void button1_Click(object sender, EventArgs e)
{
GetCharge(); GetChargeEach(); txtNowAllCharge.Text = (Convert.ToDouble(txtChargeWater.Text) + Convert.ToDouble(txtChargePower.Text) + Convert.ToDouble(txtChargeGas.Text)).ToString("0.00");
WriteBinaryFile();
bylProbar.Visible = true;
btnCalc.Visible = false;
timer1.Interval =1;
timer1.Start();
c1 = bylProbar.Position;
} private void GetCharge()
{
txtChargeWater.Text = Cost(txtNowWater.Text, txtLastWater.Text, txtFeeWater.Text);
txtChargePower.Text = Cost(txtLastPower.Text, txtNowPower.Text, txtFeePower.Text);
txtChargeGas.Text = Cost(txtLastGas.Text, txtNowGas.Text, txtFeeGas.Text);
} private void GetChargeEach()
{
txtChargeWaterEach.Text = EachCost(txtChargeWater.Text);
txtChargePowerEach.Text = EachCost(txtChargePower.Text);
txtChargeGasEach.Text = EachCost(txtChargeGas.Text);
} private string Cost(string now, string last, string fee)
{
double r = 0.0;
try { r = Convert.ToDouble(now) - Convert.ToDouble(last); }
catch (Exception e) { statusStrip1.Text = e.ToString(); }
return (r * Convert.ToDouble(fee)).ToString("0.00");
} private string EachCost(string charge)
{
if (charge != string.Empty)
return (double.Parse(charge) / unitCount).ToString("0.00");
else return "";
}
private void Form1_Load(object sender, EventArgs e)
{
toolStripStatusLabel1.Text += " 今天是 " + DateTime.Today.ToLongDateString() + " " + "星期" + "日一二三四五六".Substring((int)System.DateTime.Now.DayOfWeek, 1); ;
} private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
unitCount = 1;
GetChargeEach();
} private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
unitCount = 2;
GetChargeEach();
} private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
unitCount = 3;
GetChargeEach();
} private void timer1_Tick(object sender, EventArgs e)
{
if (c1 == bylProbar.PositionMax)
{
p1 = 0;
timer1.Stop();
bylProbar.Visible = false;
btnCalc.Enabled = true;
btnCalc.Visible = true;
}
else
if (c1 == bylProbar.PositionMin)
{
p1 = 1;
}
c1 += p1; bylProbar.Text = c1.ToString() + " %";
bylProbar.Position = c1;
}
}
}
{
this.Cursor = Cursors.WaitCursor; //获取鼠标的形状,为沙漏形状
//让界面上的进度条滚动
progressBar1.Style = ProgressBarStyle.Marquee;
//通过异步调用长时间查询
MethodInvoker mi = new MethodInvoker(DoSomething);
mi.BeginInvoke(null, null);
}private void DoSomething()
{
DateTime KaiShi = DateTime.Now; Pffx_hz(); //这个是一个查询的函数,主要是这里要的时间很长;
Pffx_gyd();//这个是另外一个查询的函数,主要也是这里要的时间很长; DateTime Eend = DateTime.Now;
TimeSpan XiangCha = Eend - KaiShi;
string zhixing = XiangCha.Minutes + "分" + XiangCha.Seconds + "秒";
MessageBox.Show("此查询一共用时:" + zhixing, "提示"); //执行完耗时的查询,通过异步调用,停止滚动条
BeginInvoke(new MethodInvoker(Finish)) ;
}private void Finish()
{
this.Cursor = Cursors.Default; //获取鼠标为正常形状 progressBar1.Style = ProgressBarStyle.Blocks;//进度条还原,不再滚动
}
自制ProgressBar,我看到了,多线程??厄........
然后在写一个回调方法,当异步线程完成后会调用这个方法,在此方法中可以进行一些完成够的操作,比如处理数据和取消进度条
delegate void QueryDelegate(); //委托类型根据你方法的返回类型
//回调方法
static void CallbackMethod(IAsyncResult ar)
{
//查询完成够,调用此方法,结束异步调用
QueryDelegate dlgt = (AsyncDelegate) ar.AsyncState;
dlgt.EndInvoke(ar);
//这里可以加一些其他操作,比如取消进度条。
}
private void button1_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
DateTime KaiShi = DateTime.Now; //使用委托的异步方法,并给他传递一个回调方法,但异步查询完成时可以调用这个方法。
QueryDelegate dlgt = new QueryDelegate(Pffx_hz);
IAsyncResult ar = dlgt.BeginInvoke(new AsyncCallback(CallbackMethod));
//这里可以显示一个进度条等代码
DateTime Eend = DateTime.Now;
TimeSpan XiangCha = Eend - KaiShi;
string zhixing = XiangCha.Minutes + "分" + XiangCha.Seconds + "秒";
this.Cursor = Cursors.Default;
MessageBox.Show("此查询一共用时:" + zhixing, "提示");
}
也还有其他方法,手动使用线程池或使使用Thread来控制。道理都一样。用一个新线程运行查询。然后原线程显示一个进度。
当查询完成,通知原线程,取消进度!上面大概写的。不一定对。MSDN上有异步和多线程的例子
没一段时间查询一次查询情况,但你程序里要知道要查询多少记录,已经查询了多少
然后定时返回这些信息。来控制进度条
//
//编译: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把那个空循环换成你自己的内容.
A线程显示一个等待的包含动作图片(搞个转动的GIF)的窗口
B线程包含你的实际操作A线程开启B线程
B线程结束的时候 设置A线程窗口中的显示内容
http://download.csdn.net/source/480743去下吧..
http://p.blog.csdn.net/images/p_blog_csdn_net/kevery_net/405371/o_ec123.JPG为你这题目而做的事例........... 我已上传....
http://download.csdn.net/source/480743 去下吧..