關於下載更新數據於滾動條類似PrograssBar控制項的問題,我網上搜索了下,總體結合大致採用微軟的定時器Timer控制項更新數據。 在網上發現了典型的例子是使用TImer定時器和BackgroundWorker組件的結合更新PrograssBar進度的操作,網址如下: http://www.cnbl ...
關於下載更新數據於滾動條類似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 !