Select to view content in your preferred language

Desktop Console Applicaiton does not close window in 10.1

3439
8
06-11-2013 05:01 AM
JoseSanchez
Frequent Contributor
Hi all

I wrote a Desktop Console Application in 9.3 and it works fine.  Then I upgraded to  10.0 and it works fine too.

I copied the application  to a Windows server 2008R2 with ArcGIS 10.1 recompiled, etc, and now I have a problem when I run the .exe alone, the Console Window does not close, it stays open.

I am using VS 2010 VB.NEt and if I run the application from VS 2010 IDE it works fine, but running it as a stand alone program the Console Window does not close.


I am checking the code and commenting the following line:

      pFeatureClass = gputilities.OpenFeatureClassFromString(pathOutput) 

the stand alone program works fine.

Anyone with a similar problem


  'ESRI License Initializer generated code.
        If (Not m_AOLicenseInitializer.InitializeApplication(New esriLicenseProductCode() {esriLicenseProductCode.esriLicenseProductCodeAdvanced}, _
        New esriLicenseExtensionCode() {})) Then
            Console.WriteLine(m_AOLicenseInitializer.LicenseMessage())
            Console.WriteLine("This application could not initialize with the correct ArcGIS license and will shutdown.")
            m_AOLicenseInitializer.ShutdownApplication()
            Return
        End If



        '
        ' Initialize the Geoprocessor
        '
        GP = New ESRI.ArcGIS.Geoprocessing.GeoProcessor()


        gputilities = New GPUtilitiesClass()

        '
        ' Read App settings
        '
        prefixOutput = System.Configuration.ConfigurationManager.AppSettings.Item("prefixOutput")
        prefixOutputOwner = System.Configuration.ConfigurationManager.AppSettings.Item("prefixOutputOwner")
        outputFC = System.Configuration.ConfigurationManager.AppSettings.Item("outputFC")
        ' FFeature class 
        pathOutput = prefixOutput & prefixOutputOwner & outputFC

        '
        ' create points
        '
        Console.WriteLine("Start")


      

        Try
            '   pathOutput = SDE Feature Class where the program creates points 
            pFeatureClass = gputilities.OpenFeatureClassFromString(pathOutput)

            Console.WriteLine("Create Points")

            ' CreatePoints()

            pFeatureClass = Nothing

        Catch ex As Exception

        End Try





        m_AOLicenseInitializer.ShutdownApplication()
        Console.WriteLine("End")


[ATTACH=CONFIG]25192[/ATTACH]
0 Kudos
8 Replies
JoseSanchez
Frequent Contributor
Hi all

It looks like this code does not work in 10.1, at least it does nto close the console window when running a program created withVS 21010, ArcGIS 10.1 and .Net 3.5.


[C#]
namespace ConsoleApplication1
{
  class Program
  {
    [STAThread]
    static void Main(string[] args)
    {
       // ...
    }
  }
}
0 Kudos
JoseSanchez
Frequent Contributor
Changing the type of thread in the source code from STAThread  to MTAthread worked.


http://support.microsoft.com/kb/828988
0 Kudos
nicogis
MVP Alum
But remember that:

In the Single-Threaded Apartment (STA), a single thread is dedicated to execute the methods of the object. In such an arrangement, method calls from threads outside of the apartment are marshalled and automatically queued by the system (via a standard Windows message queue). Thus, the COM run-time provides automatic synchronization to ensure that each method call of an object is always executed to completion before another is invoked. The developer therefore does not need to worry about thread locking or race conditions.

In the Multiple Threaded Apartment (MTA), the COM run-time provides no synchronization, and multiple threads are allowed to call COM objects simultaneously. COM objects therefore need to perform their own synchronization to prevent simultaneous access from multiple threads from causing a race condition. Calls to an MTA object from a thread in an STA are also marshaled.


and model ArcObjects threading model: http://resources.esri.com/help/9.3/arcgisengine/dotnet/2c2d2655-a208-4902-bf4d-b37a1de120de.htm

As an ArcObjects developer, this means that if your application is not initialized as a single threaded application, the .NET framework will create a special single threaded apartment (STA) thread for all ArcObjects since they are marked as STA. This will cause a thread switch to this thread on each call from the application to ArcObjects. In turn, this forces the ArcObjects components to marshall each call, and eventually it may be about 50 times slower for a call to the COM component. Fortunately, this can be avoided by simply marking the main function as [STAThread].
0 Kudos
LeoDonahue
Deactivated User
Is it just me, or was this post completely different 10 min ago?  I looked at this yesterday and saw an empty main method, with no code at all.  Now it is totally different.
0 Kudos
JoseSanchez
Frequent Contributor
Thank you for your answer Domenico. 

But how do you fix the problem of the Console Window that does not close when running the application as a stand alone .exe with STA instead of MTA? 

Is there a solution using STA in 10.1 or is it a bug?


Regards





But remember that:

In the Single-Threaded Apartment (STA), a single thread is dedicated to execute the methods of the object. In such an arrangement, method calls from threads outside of the apartment are marshalled and automatically queued by the system (via a standard Windows message queue). Thus, the COM run-time provides automatic synchronization to ensure that each method call of an object is always executed to completion before another is invoked. The developer therefore does not need to worry about thread locking or race conditions.

In the Multiple Threaded Apartment (MTA), the COM run-time provides no synchronization, and multiple threads are allowed to call COM objects simultaneously. COM objects therefore need to perform their own synchronization to prevent simultaneous access from multiple threads from causing a race condition. Calls to an MTA object from a thread in an STA are also marshaled.


and model ArcObjects threading model: http://resources.esri.com/help/9.3/arcgisengine/dotnet/2c2d2655-a208-4902-bf4d-b37a1de120de.htm

As an ArcObjects developer, this means that if your application is not initialized as a single threaded application, the .NET framework will create a special single threaded apartment (STA) thread for all ArcObjects since they are marked as STA. This will cause a thread switch to this thread on each call from the application to ArcObjects. In turn, this forces the ArcObjects components to marshall each call, and eventually it may be about 50 times slower for a call to the COM component. Fortunately, this can be avoided by simply marking the main function as [STAThread].
0 Kudos
EricLussier
Occasional Contributor
Thank you for your answer Domenico. 

But how do you fix the problem of the Console Window that does not close when running the application as a stand alone .exe with STA instead of MTA? 

Is there a solution using STA in 10.1 or is it a bug?


Regards


Any answer to that question so far ?

Thanks
0 Kudos
JoseSanchez
Frequent Contributor
The only solution I found is using MTA threads instead of STA threads

http://support.microsoft.com/kb/828988


May be in the next ArcGIS 10.1 SP this issue will be fixed.
0 Kudos