Select to view content in your preferred language

Multi-threading in ArcObjects...

1387
6
11-08-2010 09:14 PM
UjjwalNigam
Regular Contributor
Hi,
I'm trying to implement multi-threading in my AO code but it fails, after a long wait, with a RPC_ESERVER Fault error message.
Here's how I'm doing it:
1. Create an ICommand. OnClick calls another method (say Start())

2. Start() intantiates the IApplication/IMxDocument and other AO vars and finally creates a Windows form which has a button (say OK) to being processing.

3. When I click OK button, I start another thread, which has all the ArcObjects related processing. The processing is happening with shared variables and some private variables (private to the method). This  thread also updates the progress bar.

With respect to the UI update I understand it should not be happening on the worker thread, but why does the processing take enormous amount of time even for a single selection and then eventually fail.

Any ideas?
0 Kudos
6 Replies
MattMoyles
Occasional Contributor
I'm not entirely sure what you are trying to do here but ESRI has some tips for writing multi-threaded code in this article.

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//000100000100000000

The GeoProcessor.ExecuteAsync is something new that you may be able to make use of.
0 Kudos
UjjwalNigam
Regular Contributor
Hello Matt,
In my application, the background process takes a long time to finish, and during that period the UI freezes (becomes white), hence the need to put the background process on a separate thread, so as to keep the UI responsive and update user about the progress.
I've created the thread and everything, but it always fails with the following message:
"Attempted to read/write protected memory. This often indicates the memory is corrupt"

This happens when I'm trying to convert a selection to an interim PGDB...
0 Kudos
RichardWatson
Deactivated User
Multi-threading is hard and doing it with ArcObjects is harder because of the COM STA model used.  Avoid it if you can.  In the past when I have searched the forums for this and found very little.

Obviously, you have a memory corruption issue.  What you didn't say is what language your code is written in or what exactly your code is doing.  For example, C++ is more powerful but also adds risks because there is no VM/CLR. 

What is the address of the memory location?  It is 0, is it in the OS protected space, it an address that was previously valid and if so what was previously at that address?  Debugging these problems gets deep fast.

Is the problem yours or ESRI's?  Hard to say.
0 Kudos
UjjwalNigam
Regular Contributor
Hmmm...
I even tried the ESRI suggested way of updating the UI using a delegate...but even that doesn't work!
I really wish if somebody could post the code related to multi-threading...much appreciated!!!

I will keep trying in the meanwhile...
0 Kudos
UjjwalNigam
Regular Contributor
OK...
I was able to do it without having a Windows form in the application, but the moment I call the form, I get the following loader lock message:

"LoaderLock was detected.
Message: Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang."


... and I am unable to proceed further!

I tried marking the form's Main method as <STAThread()> _, but still the same issue...

PS: I am working in VB.Net with VS 2005 & ArcGIS 9.2
0 Kudos
UjjwalNigam
Regular Contributor
OK...
I could finally make it working.
I am not sure about the AO code being extremely slow in a multi-threading model, as it is discussed in the following ESRI article:
http://edndoc.esri.com/arcobjects/9.2/NET/2c2d2655-a208-4902-bf4d-b37a1de120de.htm

This is how I do it:
PS: Names are as per the attached code file. I use VB.Net & VS 2005

1. Declare a delegate, a UI update method (UpdateUI) and a wrapper method (UpdateUIWrapper) to the UI update method in the form class.

2. Within a module, define a method say Start, this mainly instantiates the form; and another "friend" method UpdateFormUI, this being the main method for carrying out all the processing.

3. Within the button click method, initialize a new thread, pointing to the UpdateFormUI method defined in the module above.

Refer the attached zip for code level details. My form contained a progress bar and a button.

Hope it helps!

Cheers
0 Kudos