using System;
using System.ComponentModel;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.Geodatabase;
//Author: Domenico Ciavarella
//www.studioat.it
namespace Studioat.ArcEngine.Forms
{
public partial class frmBW : Form
{
BackgroundWorker backGroundWorker = null;
enum operation { start, stop };
public IFeatureClass FeatureClass
{
get;
set;
}
#region Ctor
public frmBW()
{
InitializeComponent();
}
public frmBW(IFeatureClass featureClass):this()
{
FeatureClass = featureClass;
}
#endregion Ctor
private void btnStart_Click(object sender, EventArgs e)
{
Operation(operation.start);
RunWorker();
}
private void btnStop_Click(object sender, EventArgs e)
{
backGroundWorker.CancelAsync();
}
#region private method
void Operation(operation op)
{
btnStart.Enabled = (op == operation.stop);
btnStop.Enabled = (op == operation.start);
InitializeProgressBar();
}
void InitializeProgressBar()
{
progressBar.Maximum = 100;
progressBar.Minimum = 0;
progressBar.Value = 0;
}
void RunWorker()
{
backGroundWorker = new BackgroundWorker();
backGroundWorker.WorkerReportsProgress = true;
backGroundWorker.WorkerSupportsCancellation = true;
backGroundWorker.DoWork += new DoWorkEventHandler(doWork);
backGroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(runWorkerCompleted);
backGroundWorker.ProgressChanged += new ProgressChangedEventHandler(progressChanged);
backGroundWorker.RunWorkerAsync(FeatureClass);
}
#endregion private method
#region BackgroundWorker
void doWork(object sender, DoWorkEventArgs e) {
BackgroundWorker senderWorker = sender as BackgroundWorker;
IFeatureClass featureClass = e.Argument as IFeatureClass;
int featureCount = featureClass.FeatureCount(null);
IFeatureCursor featureCursor = featureClass.Search(null, true);
IFeature feature = featureCursor.NextFeature();
int i = 0;
while (feature != null)
{
//////////////////////////////////////
//////////////here your work
//System.Threading.Thread.Sleep(1000);
//////////////////////////////////////
++i;
int progressInPercent = (int)(((decimal)(i) / (decimal)featureCount) * 100);
backGroundWorker.ReportProgress(progressInPercent, feature.OID);
if(senderWorker.CancellationPending) {
e.Cancel = true;
break;
}
feature = featureCursor.NextFeature();
}
ComReleaser.ReleaseCOMObject(featureCursor);
}
void runWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
lblStatus.Text = "work was cancelled.";
else
lblStatus.Text = "work finished processing request.";
Operation(operation.stop);
}
void progressChanged(object sender, ProgressChangedEventArgs e) {
progressBar.Value = e.ProgressPercentage;
lblStatus.Text = string.Format("working on OID of: {0}",e.UserState);
}
#endregion
}
}
Qui puoi scaricare la soluzione in VS2008 MapControlBW
Nessun commento:
Posta un commento