VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之事件式实时下载更新滚动条进度数据

  关于下载更新数据于滚动条类似PrograssBar控件的问题,我网上搜索了下,总体结合大致采用微软的定时器Timer控件更新数据。

  在网上发现了典型的例子是使用TImer定时器和BackgroundWorker组件的结合更新PrograssBar进度的操作,网址如下:

  http://www.cnblogs.com/jaxu/archive/2011/08/05/2128811.html

  但是使用Timer定时器更新滚动条并非实时更新数据,顶多只是尽量模拟程序执行的步骤,本篇文章采用另一种下载更新滚动条数据的方式来实现实时更新数据,结合WebService网络服务和线程执行操作。

  •   关于WebService的引用大致分为组件引用和网址引用,如图:
  •   关于WebService的部署和发布,结合本地IIS方式,如图:

  

  (1)主程序入口:

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  //Application.Run(new Form1());
  System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(UpdateLoading));
  thread.Start();
}

static void UpdateLoading()
{
  ConvMyReord.Uil.Welcome.DownLoader downLoader = new Uil.Welcome.DownLoader();
  downLoader.Show();
  downLoader.Start();
}

  (2)DownLoader类:

private void downLoading_DownLoadingCancelEvnet() 
{
  if (_downLoading != null && _downLoading.GetDialogResult == DialogResult.No)
  Application.Exit();
}

public void Start()
{
  if (_downLoading == null) _downLoading = new DownLoading();
  _downLoading.Dock = DockStyle.Fill;
  this.pnlMain.Controls.Add(_downLoading);
  _downLoading.DownLoadingCancelEvnet += downLoading_DownLoadingCancelEvnet;
  _downLoading.StartDownLoading();
}

  (3)自定义控件:DownLoading

public delegate void DownLoadingCancel();
public event DownLoadingCancel DownLoadingCancelEvnet;
public DialogResult GetDialogResult {get;set;}

private void btnCancel_Click(object sender, EventArgs e)
{
  if(DownLoadingCancelEvnet != null)
  {
    this.GetDialogResult = DialogResult.No;
    DownLoadingCancelEvnet();
  }
}

public void StartDownLoading()
{
  ConvMyReord.WebReference.ConverRecordWebService converRecordWebService
  = new WebReference.ConverRecordWebService();
  System.Data.DataSet ds = converRecordWebService.DownLoadingScoure();//调用服务方法
  DownLoadHelper downLoadHelper = new DownLoadHelper();
  downLoadHelper.DownLoadProgressEvent += downloader_onDownLoadProgress;
  downLoadHelper.StartLoading(ds, this.progressBarControl1, lblDownLoad);
}

//同步更新UI
private void downloader_onDownLoadProgress(long totalCount, long current)
{
  float percent = 0;
  if (this.InvokeRequired)
  {
    this.Invoke(new DownLoadHelper.DownLoadProgress(downloader_onDownLoadProgress), new object[] { totalCount, current });
  }
  else
  {
    if (this.progressBarControl1.Properties.Maximum == this.progressBarControl1.Position)
    {
      this.GetDialogResult = DialogResult.Yes;
      this.btnCancel.Enabled = false;
    } 
    this.progressBarControl1.Properties.Maximum = (int)totalCount;
    this.progressBarControl1.Position = (int)current;
    percent = (float)current / (float)totalCount * 100;
    this.lblDownLoad.Text = "当前补丁下载进度:" + percent.ToString() + "%";
    System.Windows.Forms.Application.DoEvents();
  }
}

  (4)DownLoadHelper帮助类:

public delegate void DownLoadProgress(long total, long current);
public event DownLoadProgress DownLoadProgressEvent;

public void StartLoading(System.Data.DataSet ds, DevExpress.XtraEditors.ProgressBarControl progressBar1, System.Windows.Forms.Label label1)
{
  if (ds == null) return;
  long totalCount = GetRowCounts(ds);
  LoadingData(totalCount, ds, progressBar1, label1);
}

private static long GetRowCounts(System.Data.DataSet ds)
{
  long count = 0;
  if (ds.Tables.Count <= 0) return count;
  for (int i = 0; i < ds.Tables.Count; i++)
  {
    System.Data.DataTable table = ds.Tables[i];
    count += table.Rows.Count;
  }
  return count;
}

public void LoadingData(long totalCount, System.Data.DataSet ds, DevExpress.XtraEditors.ProgressBarControl progressBar1, System.Windows.Forms.Label label1)
{
  if (totalCount <= 0 || ds.Tables.Count <= 0) return;
  long count = 0;
  for (int i = 0; i < ds.Tables.Count; i++)
  {
    System.Data.DataTable table = ds.Tables[i];
    if (table == null || table.Rows.Count <= 0) continue;
    for (int j = 0; j < table.Rows.Count; j++)
    {
      ++count;
      System.Windows.Forms.Application.DoEvents();
      if (DownLoadProgressEvent != null)
      DownLoadProgressEvent(100, (100/totalCount) * count);
       System.Threading.Thread.Sleep(100);
    }
  }
}

效果图:

 

  以上是我前段时间自己编的部分程序,考虑从内存空间和效率上都存在不足,本篇主要是让大家了解事件式实时更新滚动条数据的编程思路,不足之处,请留言赐教。
  A young idler  ~  an old beggar !


相关教程