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

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

 

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

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

  澳门凯旋门游戏网址 2

澳门凯旋门游戏网址 3

  (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

澳门凯旋门游戏网址 4

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

private void btnCancel_Click(object sender, EventArgs e)
{
  if(DownLoadingCancelEvnet != null)
  {
可是利用Timer机械漏刻更新滚动条并非实时更新数据【澳门凯旋门游戏网址】。    this.GetDialogResult = DialogResult.No;
    DownLoadingCancelEvnet();
  }
}

可是利用Timer机械漏刻更新滚动条并非实时更新数据【澳门凯旋门游戏网址】。public void StartDownLoading()
{
  ConvMyReord.WebReference.ConverRecordWebService
converRecordWebService
  = new WebReference.ConverRecordWebService();
  System.Data.DataSet ds =
converRecordWebService.DownLoadingScoure();//调用服务方法
  DownLoadHelper downLoadHelper = new DownLoadHelper();
  downLoadHelper.DownLoadProgressEvent +=
downloader_onDownLoadProgress;
可是利用Timer机械漏刻更新滚动条并非实时更新数据【澳门凯旋门游戏网址】。  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
可是利用Timer机械漏刻更新滚动条并非实时更新数据【澳门凯旋门游戏网址】。  {
    if (this.progressBarControl1.Properties.Maximum ==
this.progressBarControl1.Position)
    {
      this.GetDialogResult = DialogResult.Yes;
      this.btnCancel.Enabled = false;
    }
可是利用Timer机械漏刻更新滚动条并非实时更新数据【澳门凯旋门游戏网址】。    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);
    }
  }
}

效果图:

澳门凯旋门游戏网址 5

 

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

相关文章