Hi!
I have just stumbled across the Backgroundworker object, and it seems like the tool I'm looking for to make my GUI responding while performing calculations. I am writing IO plugins for ArcGIS.
I am doing some data processing outside ArcGIS, which works fine using the backgroundworker. But when I'm inserting the data into ArcGIS, the backgroundworker seems to increase the duration time by a factor 9 or so. Placing the processing code outside the DoWork method, increases the performance by a factor 9.
I have read about this several places on the net, but I have no experience in multithreaded programming and the terms like STA and MTA means nothing to me. link text I have also tried to use a simple implementation of threading, but with similar results.
Does anyone know what I can do to be able to use ArcGIS processing and maintaining a responsive GUI?
EDIT: I have included a sample of my interaction with the background worker. If I put the code located in the StartImporting method in the cmdStart_Click method, it executes much faster.
private void StartImporting(object sender, DoWorkEventArgs e)
{
DateTime BeginTime = DateTime.Now;
// Create a new report object.
SKLoggingObject loggingObject = new SKLoggingObject("log.txt");
loggingObject.Start("Testing.");
SKImport skImporter = new SKImport(loggingObject);
try
{
// Read from a text box - no writing.
skImporter.Open(txtInputFile.Text);
}
catch
{
}
SKGeometryCollection convertedCollection = null;
// Create a converter object.
GEN_SK2ArcGIS converter = new GEN_SK2ArcGIS(loggingObject);
// Convert the data.
convertedCollection = converter.Convert(skImporter.GetGeometry());
// Create a new exporter.
ArcGISExport arcgisExporter = new ArcGISExport(loggingObject);
// Open the file.
// Read from a text box - no writing.
arcgisExporter.Open(txtOutputFile.Text);
// Insert the geometry collection.
try
{
arcgisExporter.Insert(convertedCollection);
}
catch
{
}
TimeSpan totalTime = DateTime.Now - BeginTime;
lblStatus.Text = "Done...";
}
private void ChangeProgress(object sender, ProgressChangedEventArgs e)
{
// If any message was passed, display it.
if (e.UserState != null && !((string)e.UserState).Equals(""))
{
lblStatus.Text = (string)e.UserState;
}
// Update the progress bar.
pgStatus.Value = e.ProgressPercentage;
}
private void ImportDone(object sender, RunWorkerCompletedEventArgs e)
{
// If the process was cancelled, note this.
if (e.Cancelled)
{
pgStatus.Value = 0;
lblStatus.Text = "Operation was aborted by user...";
}
else
{
}
}
private void cmdStart_Click(object sender, EventArgs e)
{
// Begin importing the sk file to the geometry collection.
// Initialise worker.
bgWorker = new BackgroundWorker();
bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ImportDone);
bgWorker.ProgressChanged += new ProgressChangedEventHandler(ChangeProgress);
bgWorker.DoWork += new DoWorkEventHandler(StartImporting);
bgWorker.WorkerReportsProgress = true;
bgWorker.WorkerSupportsCancellation = true;
// Start worker.
bgWorker.RunWorkerAsync();
}
private void cmdCancel_Click(object sender, EventArgs e)
{
bgWorker.CancelAsync();
}
Kind Regards, Casper