The problem that you are having is that "SendStringToExecute" is an Asynchronous command, so you are trying to save and close your document while the comand is being executed.
Time ago, I found a class from caddzone.com that envolves the synchronous "acedCmd" command.
Here you have the code, try to use it instead the "SendStringToExecute".
// CommandLine.cs Copyright (c) 2006 www.caddzone.com
//
// Original source location:
//
// http://www.caddzone.com/CommandLine.cs
//
// LICENSE:
//
// This software may not be published or reproduced in any
// form, without the express written consent of CADDZONE.COM
//
// Fair use:
//
// Permission to use this software in object code form, for
// private software development purposes, and without fee is
// hereby granted, provided that the above copyright notice
// appears in all copies and that both that copyright notice
// and limited warranty and restricted rights notice below
// appear in all supporting documentation.
//
// CADDZONE.COM PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// CADDZONE.COM SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. CADDZONE.COM
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
using
System;
using
System.Security;
using
System.Runtime.InteropServices;
using
Autodesk.AutoCAD.ApplicationServices;
using
Autodesk.AutoCAD.Runtime;
using
System.Collections;
using
Autodesk.AutoCAD.Geometry;
using
Autodesk.AutoCAD.DatabaseServices;
using
Autodesk.AutoCAD.EditorInput;
using
AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using
System.Collections.Generic;
namespace
CADFuncs
{
[
SuppressUnmanagedCodeSecurity]
publicstaticclassCommandLine
{
conststring ACAD_EXE = "acad.exe";
constshortRTSTR = 5005;
constshortRTNORM = 5100;
constshortRTNONE = 5000;
constshortRTREAL = 5001;
constshortRT3DPOINT = 5009;
constshortRTLONG = 5010;
constshortRTSHORT = 5003;
constshortRTENAME = 5006;
constshort RTPOINT = 5002; /*2D point X and Y only */staticDictionary<Type, short> resTypes = newDictionary<Type, short>();
staticCommandLine()
{
resTypes[
typeof(string)] = RTSTR;
resTypes[
typeof(double)] = RTREAL;
resTypes[
typeof(Point3d)] = RT3DPOINT;
resTypes[
typeof(ObjectId)] = RTENAME;
resTypes[
typeof(Int32)] = RTLONG;
resTypes[
typeof(Int16)] = RTSHORT;
resTypes[
typeof(Point2d)] = RTPOINT;
}
staticTypedValue TypedValueFromObject(Objectval)
{
if (val == null)
thrownewArgumentException("null not permitted as command argument");
shortcode = -1;
if (resTypes.TryGetValue(val.GetType(), outcode) && code > 0)
returnnewTypedValue(code, val);
thrownewInvalidOperationException("Unsupported type in Command() method");
}
publicstaticint Command(paramsobject[] args)
{
if (AcadApp.DocumentManager.IsApplicationContext)
thrownewInvalidCastException("Invalid execution context");
intstat = 0;
intcnt = 0;
using (ResultBuffer buffer = newResultBuffer())
{
foreach (object o inargs)
{
buffer.Add(TypedValueFromObject(o));
++cnt;
}
if(cnt > 0)
{
stat = acedCmd(buffer.UnmanagedObject);
}
}
returnstat;
}
[
DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl)]
externstaticint acedCmd(IntPtrresbuf);
}
/*public class Examples
{
// Sample member functions that use the Command() method.
public static int ZoomExtents()
{
return CommandLine.Command("._ZOOM", "_E");
}
public static int ZoomCenter(Point3d center, double height)
{
return CommandLine.Command("._ZOOM", "_C", center, height);
}
public static int ZoomWindow(Point3d corner1, Point3d corner2)
{
return CommandLine.Command("._ZOOM", "_W", corner1, corner2);
}
}*/
}
Luis Alberto Manero, Geograma.com