110,577
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
}
//======================================================================
//
// 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;