声明:仅做自己学习整理用,内容拷贝自ArcEngine SDK开发文档

Running a geoprocessing tool using background geoprocessing

Summary This topic explains how to programmatically execute a geoprocessing tool or geoprocessing model tool in the background of an ArcGIS application so that the foreground of the application remains responsive to the user. This means that an ArcGIS control (for example, MapControl) or ArcMap remains responsive to user interaction while a tool is executing. 
这句话的意思应该是单独设置线程跑GP tool而不影响前台程序的用户交互。

In this topic

  • Geoprocessing
  • Background geoprocessing
  • Submitting a tool
  • Checking tool execution status
    • Geoprocessor events
    • ToolExecuting event
    • ProgressChanged event
    • ToolExecuted event
    • IGeoProcessorResult
  • Canceling a tool
  • Layer and data usage during tool execution
  • Edit sessions and background geoprocessing
    • Start editing then execute in the background
    • Execute in the background then start editing

Geoprocessing

Since ArcGIS 9.2, a geoprocessing job (tool or model tool) can run programmatically using the Geoprocessor.Execute method. The line of code immediately following the Geoprocessor.Execute method cannot be executed until the geoprocessing tool has completed. Consequently, when a geoprocessing tool takes a significant amount of time, the application pauses and is unresponsive to the user.
For more information on foreground execution, see How to run a geoprocessing tool.
If a geoprocessing task is in a service executed on a server, the mechanism to execute the task and listen for feedback is described in How to work with geoprocessing services. To run multiple geoprocessing tasks asynchronously to each other, use multi-threaded execution on the server.

Background geoprocessing

Background geoprocessing is new at ArcGIS 10. Using the Geoprocessor.ExecuteAsync method, you can execute a tool or model tool in the background of an ArcGIS application. This means that an ArcGIS control (for example, MapControl, PageLayoutControl, GlobeControl, or SceneControl) remains responsive to user interaction while a tool is executing in a background process. In other words, data can be viewed and queried while a tool is executing.
Executing a tool using background geoprocessing, requires the following tasks:
  • Submitting the tool for execution.
  • Checking the tool's execution status.

Submitting a tool

Create the geoprocessor, then submit the parameterized tool to the ExecuteAsync method as shown in the following code example:

[C#]

ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.GeoProcessor();
gp.OverwriteOutput = true;
//Register to receive the geoprocessor event when the tools have completed execution.
gp.ToolExecuted += new EventHandler < ESRI.ArcGIS.Geoprocessor.ToolExecutedEventArgs> (gpToolExecuted);
IGeoProcessorResult2 gpResult = gp.ExecuteAsync("CopyFeatures", parameters)asIGeoProcessorResult2;
Set the OverwriteOutput property on the geoprocessor to true if you expect to be running a tool many times with the same output parameter.
The ExecuteAsync method submits a tool to a geoprocessing queue that exists in the current process, then the tool'sIGeoProcessorResult.Status property becomes esriJobStatus.esriJobWaiting. 
A tool on the geoprocessing queue will not start executing until the event that added the tool to the queue has been fully processed. For example, if a button click event submits a tool, searches a features class, then changes some on-screen text, the tool starts executing after the on-screen text has changed.
Tools are executed in a background process in the order in which they were added to the queue. However, tools cannot be added to the queue unless the input to the tool exists when the tool is submitted. To work around this limitation, consider one of the following: 
  • Chaining the tools within a geoprocessing model, which can be run as described in Consuming a geoprocessing model tool in .NET. A model is treated as a single tool and the existence of any intermediate data will not be validated by the geoprocessor.
  • Maintaining your geoprocessing queue as shown in Executing geoprocessing tools in the background.
The input and output parameters for each geoprocessing tool are documented in the toolbox to which a tool belongs. For a description of each toolbox, see Working with geoprocessing tool documentation and the other topics in the Geoprocessing tool reference section in the ArcGIS Desktop User Help system.

Checking tool execution status

The following options are available to determine whether a tool has completed or is still executing:
  • Register and listen for the Geoprocessor.ToolExecuted events and, optionally, the other events (this is the most common option).
  • Periodically query IGeoprocessorResult.

Geoprocessor events

  • The ToolExecuting event is fired by all tools before a tool is taken from the geoprocessing queue and executed in the background. At this time, the tool's IGeoProcessorResult.Status property is still esriJobStatus.esriJobWaiting.
  • The MessagesCreated event and the ProgressChanged event can fire during tool execution. These events fire depending on which tool is executing and how much data the tool is processing.
  • The ToolExecuted event is fired by all tools when background execution has stopped.
A model tool is treated as a single tool and fires one ToolExecuting event and one ToolExecuted event as previously discussed. MessagesCreated events fire describing the progress of each tool within the model.

ToolExecuting event

If you have submitted many tools, you can determine which tool has started executing as shown in the following event handler code example. Similar code can be written in any event handler to determine which tool the event applies to.

[C#]

public void gpToolExecuting(object sender, ToolExecutingEventArgs e)
{IGeoProcessorResult2 result = e.GPResult as IGeoProcessorResult2;//Determine if this is the tool to handle this event.if (result.Process.Tool.Name.Equals("CopyFeatures") && result.GetInput(0).GetAsText().Equals(@"c:\Europe_Roads\Europe_Roads") && result.GetOutput(0).GetAsText().Equals(@"c:\Europe_Roads\Europe_Roads_Copy")){//Application specific code.}
}

ProgressChanged event

The ProgressChanged event fires depending on which tool is executing and how much data it is processing. You can handle this event as shown in the following code example:

[C#]

public void gpProgressChanged(object sender, ProgressChangedEventArgs e)
{System.Windows.Forms.ProgressBar progressBar = myProgressBar;IGeoProcessorResult2 gpResult = (IGeoProcessorResult2)e.GPResult;switch (e.ProgressChangedType){case (ProgressChangedType.Show): //The tool that is running reports progress or has stopped reporting progress; make the // progress bar visible if appropriate. progressBar.Visible = e.Show;break;case (ProgressChangedType.Message): //The application does not use these, since a tool being used reports percentage progress.break;case (ProgressChangedType.Percentage): progressBar.Value = (int)e.ProgressPercentage;break;default:throw new ApplicationException("unexpected ProgressChangedEventsArgs.ProgressChangedType");break;}
}

ToolExecuted event

All tools fire the ToolExecuted event when background execution has stopped. The ToolExecutingEventArgs parameter has the return value, the status of a job and geoprocessing messages.
The IGeoProcessorResult interface always returns the value of a tool when it has finished executing. The return value is an object. Typically, this object is the path to the output dataset created or edited by a tool, but it can be other value types, such as a number or a Boolean. If an output for a tool is a multi-value parameter, the values are returned in a string that consists of multiple strings separated by a semicolon.

When a tool has executed, the return value, status of the job, and all messages can be retrieved. See the following code example:

[C#]

public void gpToolExecuted(object sender, ToolExecutedEventArgs e)
{IGeoProcessorResult2 result = e.GPResult as IGeoProcessorResult2;if (result.Status.Equals(esriJobStatus.esriJobSucceeded)){//Check that there are no information or warning messages.if (result.MaxSeverity == 0){//Get the return value.object returnValue = result.ReturnValue;//Application specific code, //for example, find the layer to which this return value corresponds.}else{//Application specific code.}}else{//Get all messages.IGPMessages msgs = result.GetResultMessages();for (int i = 0; i < result.MessageCount; i++){IGPMessage2 msg = msgs.GetMessage(i)as IGPMessage2;//Application specific code.}}
}
For general information about message severity levels, see Geoprocessing messages.

IGeoProcessorResult

Your application can have critical sections of code that must not be interrupted or must only be interrupted at known points. Rather than be interrupted by geoprocessor events, you can maintain the IGeoProcessorResult object returned by the ExecuteAsync method and, at known times, check its job status to determine whether a tool has executed successfully.

Canceling a tool

A tool can be canceled any time, from the foreground application or from a GeoProcessor event handler. To cancel a tool, call IGeoProcessorResult2.Cancel(). While the tool is canceling, its status will be esriJobStatus.esriJobCancelling and its IGeoProcessorResult2.IsCanceled property will be true. 
Confirm that the tool is completely canceled in a ToolExecuted event handler, by testing that the IGeoProcessorResult2.Status is esriJobStatus.esriJobCancelled.

Layer and data usage during tool execution 

When a tool is waiting to execute, an application is free to use data that is input to a tool or that will be overwritten by the output of a tool. 
During tool execution, the following tasks can be accomplished:
  • Changing the visibility, select ability, and connection details of a layer input to or output from an executing geoprocessing tool.
  • Searching and selecting data input to a geoprocessing tool, for example, using the IFeatureLayer.Search and IFeatureClass.Select methods.
During tool execution, the following tasks succeed or fail depending on what the geoprocessing tool is currently doing with the data:
  • Editing table rows of input or output data in or out of an edit session.
  • Using the IFeatureLayer.Search and IFeatureClass.Select methods on output data. The set of table rows returned is undefined, since the table is in the process of being populated.
When these methods are run on output data from a geoprocessing tool, the geoprocessing tool can fail and return an esriJobStatus.esriJobFail status.
During tool execution, you cannot obtain a schema lock on input and output data, and schema changes made without a schema lock will not take effect.

Edit sessions and background geoprocessing

This section describes what actions the ArcGIS application can take if a tool is executed in the background while an edit session is in the foreground or, conversely, if an edit session is started in the foreground while geoprocessing in the background.

Start editing then execute in the background

Before executing a geoprocessing tool, check whether the user is editing the dataset's input to and output from the tool. The best practice—which suits the majority of ArcGIS applications—is, if editing, to prompt the user to save edits and stop editing. This can be achieved in a ToolExecuting event handler as shown in the following code example:

[C#]

public void gpToolExecuting(object sender, ToolExecutingEventArgs e)
{IGeoProcessorResult2 result = e.GPResult as IGeoProcessorResult2;//Get the single feature class input parameter.IGPParameter parameter = result.Process.InputParameters.get_Element(0)asIGPParameter;string parameterPath = "";if (parameter.DataType.DisplayName.Equals("FeatureClass")){parameterPath = parameter.Value.GetAsText();}//Check whether this dataset is being edited.ESRI.ArcGIS.Controls.EngineEditor engineEditor = newESRI.ArcGIS.Controls.EngineEditor();if (engineEditor.EditState == esriEngineEditState.esriEngineStateEditing &&engineEditor.EditWorkspace != null){IEnumDatasetName datasetNames = engineEditor.EditWorkspace.get_DatasetNames(esriDatasetType.esriDTAny);IDatasetName name = datasetNames.Next();while (name != null){string path = engineEditor.EditWorkspace.PathName + @"\" + name.Name;if (path.Equals(parameterPath)){//This datset is being edited. Prompt the user to stop editing.}}}
}
If editing is not stopped, the geoprocessing tool fails. This failure is confirmed by checking for the esriJobStatus.esriJobFail status and reading the IGeoProcessorResult messages in a ToolExecuted event handler.
Another option is to let the user continue editing and cancel the tool or let it fail. You can then revert to executing the geoprocessing tool in the foreground. In the ToolExecuting event handler, call IGeoProcessorResult2.Cancel(), then in the ToolExecuted event handler, check that the tool is canceled using IGeoProcessorResult2.Status = esriJobStatus.esriJobCancelled, then use the Geoprocessor.Execute method. This foreground execution prevents the user from interacting with the application until the tool has completed. This option might be preferable to requiring the user to save or discard edits, especially if the foreground geoprocessing completes in a timely manner. All edits that the tool has made in the edit workspace are saved when editing is stopped.

Execute in the background then start editing

If your edited map contains layers and source data that are input to or output from geoprocessing tools, StartEditing will not be blocked. However, if your edited map contains only layers and source data that are the output of geoprocessing tools, StartEditing will be blocked.
Start editing is only blocked by the background process during tool execution. It is possible that your application will attempt to start editing before it can be blocked. Therefore, before the user or application starts editing data, check whether or not this data is (or will become) the input to or output from any geoprocessing tool that is queued for execution or is executing. To do this, maintain a list of IGeoProcessorResult2 objects with the result of each geoprocessing tool you have queued, then catch the IEditEvents.OnStartEditing or IWorkspaceEditEvents.OnStartEditing event.
If any tool is running or is scheduled to run, check each tool's parameters (from the list of IGeoProcessorResult2 objects) against the edited workspace, as shown in the previous code example.

See Also:

How to run a geoprocessing tool
How to work with geoprocessing services
Geoprocessing
Using geoprocessing



To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):

  • ESRI.ArcGIS.Geoprocessing
  • ESRI.ArcGIS.Geoprocessor
  • ESRI.ArcGIS.Geodatabase
Development licensing Deployment licensing
Engine Developer Kit Engine Runtime
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo

GP学习(四)—Running a geoprocessing tool using background geoprocessing相关推荐

  1. GP学习(三)—How to run a geoprocessing tool

    声明:仅做自己学习整理用,内容拷贝自ArcEngine SDK开发文档 How to run a geoprocessing tool In this topic Running a geoproce ...

  2. GP学习整理(一)—Geoprocessing assembly and Geoprocessor managed assembly

    声明:仅做自己学习整理用,内容拷贝自ArcEngine SDK开发文档 Geoprocessing assembly overview In this topic About the Geoproce ...

  3. GP学习(二)—Executing tools and Accesubg licensing0

    声明:仅做自己学习整理用,内容拷贝自ArcEngine SDK开发文档 Executing tools In this topic About roadmap to executing tools C ...

  4. GP学习(五)—ArcGIS Toolbox Reference dialog box

    声明:仅做自己学习整理用,内容拷贝自ArcEngine SDK开发文档 ArcGIS Toolbox Reference dialog box Summary The ArcGIS Toolbox R ...

  5. RTKLIB专题学习(四)---单点定位实现初识(二)

    RTKLIB专题学习(四)-单点定位实现初识(二) 今天我们来继续学习RTKLIB中单点定位的调用情况,上一篇在这里:RTKLIB专题学习(四)-单点定位实现初识(一) 1.上篇说到了调用procpo ...

  6. 鸿蒙Hi3861学习十九-DevEco Device Tool源码获取、编译、下载

    一.简介 在上一篇文章中,已经讲述了如何在Windows通过Remote SSH远程连接Linux下的DevEco Device Tool.这篇文章,来说一下关于源码的获取.编译与下载.建议先按照上一 ...

  7. C#多线程学习(四) 多线程的自动管理(线程池) (转载系列)——继续搜索引擎研究...

    在多线程的程序中,经常会出现两种情况: 一种情况:   应用程序中,线程把大部分的时间花费在等待状态,等待某个事件发生,然后才能给予响应                   这一般使用ThreadPo ...

  8. python学习四(处理数据)

    python学习四(处理数据) head first python中的一个数据处理的例子 有四个U10选手的600米成绩,请取出每个选手跑的最快的3个时间.以下是四位选手的9次成绩 James 2-3 ...

  9. PyTorch框架学习四——计算图与动态图机制

    PyTorch框架学习四--计算图与动态图机制 一.计算图 二.动态图与静态图 三.torch.autograd 1.torch.autograd.backward() 2.torch.autogra ...

最新文章

  1. clear arp-cache作用_肇庆Sylvin-2900-75-Clear
  2. hide handkerchief(hdu2104)
  3. JS判断是否出现滚动条
  4. 他言行不一屡次跳槽,还升职加薪走上了人生巅峰,全数学界都炸了......
  5. html5爱情树怎么修改,jQuery结合HTML5制作的爱心树表白动画
  6. fafu oj 1266 数数
  7. 【转】HTTP协议之multipart/form-data请求分析
  8. codeigniter中 get_instance()的应用
  9. java JDBC入门及案例演示
  10. 网管必读-常用网络命令
  11. 什么软件可以测试QQ特别关心,qq特别关心查询工具
  12. 思科CISCO常用命令汇总
  13. 计算机cmd管理员,cmd获取管理员权限的命令是什么
  14. (三)空域图像增强:像素联系和模板运算
  15. html圈小猫小游戏
  16. 易支付源码 28k支付第四方支付源码-Oreo支付系统
  17. 【论文笔记】Switching Convolutional Neural Network for Crowd Counting
  18. android 六边形简书,水波浪贝塞尔效果(六边形)
  19. 基于Spring boot框架开发的电商网站系统
  20. rust大油田分解机_睡梦中,狂风起!大棚棉被刮翻了,卷帘机也连带吹坏了......

热门文章

  1. bzoj3533 [Sdoi2014]向量集 可持久化凸包+二分
  2. 2017.9.20 回文串 思考记录
  3. 2017.9.17 相关分析 思考记录
  4. 2017.6.4 problem b 失败总结
  5. 每日小记2017.2.22
  6. bes2300 tws配对_tws 耳机春天来了!
  7. linux中Cron定时任务系统命令详解
  8. java final修改器_Java中的“ final”关键字如何工作?(我仍然可以修改对象。)...
  9. tensorflow2.0 图像处理项目_游戏爱好者应该看看这个开源项目
  10. python 将数据写入csv文件