我刚刚偶然发现了 Backgroundworker 对象,它似乎是我正在寻找的工具,可以让我的 GUI 在执行计算时做出响应。我正在为 ArcGIS 编写 IO 插件。
我正在 ArcGIS 之外进行一些数据处理,使用 backgroundworker 可以正常工作。但是当我将数据插入 ArcGIS 时,后台工作人员似乎将持续时间增加了 9 倍左右。将处理代码放在 DoWork 方法之外,可以将性能提高 9 倍。
我在网上读过这几个地方,但我没有多线程编程的经验,而且像 STA 和 MTA 这样的术语对我来说毫无意义。链接文本 我也尝试使用简单的线程实现,但结果相似。
有谁知道我可以做些什么才能使用 ArcGIS 处理和维护响应式 GUI?
编辑:我已经包含了我与后台工作人员交互的示例。如果我将 StartImporting 方法中的代码放在 cmdStart_Click 方法中,它的执行速度会快得多。
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();
}
亲切的问候,卡斯帕