Select to view content in your preferred language

Work Around for os.startfile Crashing arcmap

4943
5
Jump to solution
06-05-2015 07:38 AM
BenjaminMittler
Frequent Contributor

Hi All,

I'm currently attempting to develop an addin that creates a KML from a layer within arcmap, and then opens that KML. All seems to be going well except that every time the tool runs it crashes arcmap. I am aware that this is because i am using the "os.startfile()" command. What possible workaround can i use to get this file to open at the end of my script.

Thanks,

Ben

*Not a python expert by any means

Tags (1)
1 Solution

Accepted Solutions
KevinBell
Deactivated User

Jason Schriener at ESRI got this work around for me.

Add this def:

def run_in_other_thread(function): #conflicts w/ os.startfile will crash arcmap.  this is a workaround

    # functool.wraps will copy over the docstring and some other metadata

    # from the original function

    @functools.wraps(function)

    def fn_(*args, **kwargs):

        thread = threading.Thread(target=function, args=args, kwargs=kwargs)

        thread.start()

        thread.join()

    return fn_

Call it like this:

startfile = run_in_other_thread(os.startfile)

startfile(theFilePath)

View solution in original post

5 Replies
ToddBlanchette
Frequent Contributor

Do you want to open the KML in your current MXD in ArcMap? Or using an outside program like Google Earth?

0 Kudos
BenjaminMittler
Frequent Contributor

Todd,

Google Earth, the file actually opens when my addin is run, it just crashes arcmap in the process of doing so.

Thanks,

Ben

0 Kudos
ToddBlanchette
Frequent Contributor

Ben,

Thanks for more info.  If you're using Windows, os.startfile() shouldn't be giving you any issues, and like you said, it starts Google Earth fine (if kml/kmz is associated with that program within windows as the default program).  Is there anything else in your code after the os.startfile() is called?

0 Kudos
KevinBell
Deactivated User

Jason Schriener at ESRI got this work around for me.

Add this def:

def run_in_other_thread(function): #conflicts w/ os.startfile will crash arcmap.  this is a workaround

    # functool.wraps will copy over the docstring and some other metadata

    # from the original function

    @functools.wraps(function)

    def fn_(*args, **kwargs):

        thread = threading.Thread(target=function, args=args, kwargs=kwargs)

        thread.start()

        thread.join()

    return fn_

Call it like this:

startfile = run_in_other_thread(os.startfile)

startfile(theFilePath)

BenjaminMittler
Frequent Contributor

Thanks Kevin, worked like a charm.

I actually came across the thread with this information while researching the problem and it ended up that i was just calling it wrong.

For all others running into this error in the future this seems to be the workaround.