Select to view content in your preferred language

Using a function in QueuedTask.Run

2879
5
Jump to solution
06-08-2020 05:41 PM
BerndtNording
Regular Contributor

I'm working on converting some of my ArcMap addins to work in Pro.  Rather steep learning curve given that is no ESRI guidance at all on doing this.

I have found that you can't do anything with geodatabases without wrapping it into an asynchronous function. I managed to find some sample code and wrote a test addin that lists all the featureclasses in a geodatabase.

The code below actually works, but is really messy with the inline defined "lambda" function. I should be able to clean it up with a separate function as in the commented out lines in the code, but am having trouble properly constructing the code.

The IDE tells me QueuedTask.run wants an "action" and not the function I passed it, so I don't know where to go from there.

I also found that I needed to add "async" to the addin button's onclick override to make it work, but that was a shot in the dark as none of the documentation or samples showed this, so I don't know if this is the proper way to do it.

Friend Class Sample
    Inherits Button
    Dim m_gdbPath As String = "F:\HIES\CnmiImagery\CnmiImagery.gdb"

    Protected Overrides Async Sub OnClick()
        Dim strMsgs As String = ""
        Try
            'strMsgs += Await QueuedTask.Run(getDefs())

            strMsgs += Await QueuedTask.Run(Function()
                                                Dim msg As String = ""
                                                Using gdb As New Geodatabase(New FileGeodatabaseConnectionPath(New Uri(m_gdbPath, UriKind.Absolute)))
                                                    Dim defs As IReadOnlyList(Of TableDefinition) = gdb.GetDefinitions(Of FeatureClassDefinition)()
                                                    For Each fdsDef As FeatureClassDefinition In defs
                                                        msg += fdsDef.GetName & vbCrLf
                                                    Next
                                                End Using
                                                Return msg
                                            End Function)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        MsgBox(strMsgs)
    End Sub

    'Function getDefs() As String
    '    Dim msg As String = ""
    '    Using gdb As New Geodatabase(New FileGeodatabaseConnectionPath(New Uri(m_gdbPath, UriKind.Absolute)))
    '        Dim defs As IReadOnlyList(Of TableDefinition) = gdb.GetDefinitions(Of FeatureClassDefinition)()

    '        For Each fdsDef As FeatureClassDefinition In defs
    '            msg += fdsDef.GetName & vbCrLf
    '            'Console.WriteLine(TableString(fdsDef As TableDefinition))
    '        Next
    '    End Using
    '    Return msg
    'End Function
End Class
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

Hi Berndt.

Have you tried like this:

strMsgs += Await QueuedTask.Run(Return getDefs())

I don't know VB Net syntax, but you must return value from your QueedTask.Run.

Another way:

            strMsgs += Await QueuedTask.Run(Function()
                                                Return getDefs()
                                            End Function)

In c# we use {} instead of Function() End Function.

About await and async. You can use Result from QueedTask.Run. Then you do not need add async.

Like this:

            strMsgs += QueuedTask.Run(Function()
                                                ....
                                            End Function).Result

I found info for c# but I think it could be suitable and for VB Net:

https://www.exprodat.com/blogs/33195/

(Enabling Edit and Continue section)

View solution in original post

5 Replies
GKmieliauskas
Esri Regular Contributor

Hi Berndt.

Have you tried like this:

strMsgs += Await QueuedTask.Run(Return getDefs())

I don't know VB Net syntax, but you must return value from your QueedTask.Run.

Another way:

            strMsgs += Await QueuedTask.Run(Function()
                                                Return getDefs()
                                            End Function)

In c# we use {} instead of Function() End Function.

About await and async. You can use Result from QueedTask.Run. Then you do not need add async.

Like this:

            strMsgs += QueuedTask.Run(Function()
                                                ....
                                            End Function).Result

I found info for c# but I think it could be suitable and for VB Net:

https://www.exprodat.com/blogs/33195/

(Enabling Edit and Continue section)

BerndtNording
Regular Contributor

Thank you very much, Gintautas!

The first suggestion didn't work, but the second one did. It could be further cleaned up as

strMsgs += Await QueuedTask.Run(Function() getDefs())

And your suggestion for getting rid of the Async modifier worked a treat. Awesome!

0 Kudos
by Anonymous User
Not applicable

Hello Berndt Nording

Great to hear that you are beginning your work migrating your add-ins to Pro, and found your way here to the group.  I saw that you mentioned that you had not found many resources yet, and wanted to make sure you are aware of these as you get started:

  • First, there is a Getting Started document on Migrating to ArcGIS Pro with links to many of the key Pro SDK resources, including documentation, the API reference, community samples, instructor-led training, etc.
  • Your question above around QueuedTask is discussed in detail in the Framework document which is a key ProConcepts document as you get started.
  • The community samples are an important resource for learning the SDK patterns, and there are 175+ solutions, and you can download all of them as a zipfile and use all of the code in your own work.
  • You can find quick links to many of the documents above from the SDK landing page.
  • Finally the Pro SDK videos from Dev Summit are an excellent resource with best practices and patterns as you get started, and this blog post includes the full list and a youtube playlist.
0 Kudos
BerndtNording
Regular Contributor

Christopher,

 Thanks for responding. Yes, I have already studied the "Migrating to ArcGIS Pro" document, In fact I was quite thrilled to find it, and then very disappointed when I looked at the content. "Migration" is not addressed at all, this is truly a "Getting Started" (from scratch) document. For those of us that have spent years learning, developing, and refining our knowledge of customizing ArcGIS UnPro (ArcMap), there is little if anything that helps us apply those hard-won skills to migrating. Clearly, while Pro is a completely different animal and many things are different, there are still many concepts and approaches that are still the exactly the same even if they are just sporting new names and terminology.

  And yes, what I have been able to do so far has been because of the community samples. Indeed, samples are the lifeblood of a GIS Engineer that needs to have many GIS skills that are not programming related, yet still needs to use programming as just another tool to accomplish a larger task by cobbling together bits and pieces borrowed from samples.

 I am looking forward to the resumption of real (not online) instructor-led training.

I have looked at several of the DevSummit videos, and even paused them to painstakingly copy some code to a file. It would be great if any code from these videos were made available digitally. Please don't mention PDF, the bane of all GIS datahounds.

by Anonymous User
Not applicable

Thanks for your feedback, Berndt, and glad you found that document.  We've tried to include the high-level topics which are normally helpful to new Pro developers as they look to migrate or get started from scratch, and these mainly are getting started concepts. Sounds like you're very familiar with many of the resources and great news that you found the samples.  

On your question on the DevSummit videos -- yes, the team has recently been making these available, and you can find the individual SDK session materials at this page:

https://github.com/esri/arcgis-pro-sdk/wiki/tech-sessions

0 Kudos