C#用COM方式打开CAD经常出现拒绝呼叫问题!求高手指点
public static void OpenCAD2012(string path,string cmd,bool closed){
const string progID = "AutoCAD.Application.18.2";
const string exePath = @"C:\Program Files\Autodesk\AutoCAD 2012 - Simplified Chinese\acad.exe";
AcadApplication acApp = null;
AcadDocument doc=null;
// Let's first check we don't have AutoCAD already running
try
{
acApp = (AcadApplication)Marshal.GetActiveObject(progID);
}
catch { }
if (acApp != null)
{
// MessageBox.Show("An instance of AutoCAD is already running.");
}
else
{
try
{
Process pr = Process.Start(exePath);
pr.WaitForInputIdle();
while (acApp == null)
{
try
{
acApp = (AcadApplication)Marshal.GetActiveObject(progID);
}
catch
{
Application.DoEvents();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Cannot create or attach to AutoCAD object: " + ex.Message);
}
}
if (acApp != null)
{
acApp.Visible = true;
acApp.Documents.Open(path, false, null);
doc = acApp.ActiveDocument;
doc.SendCommand(cmd);
// if (closed)
// doc.Close(true);
}
}
}
网上有说在启动前让进程sleep(1000)的方法,但是如果批量打开cad的还是会出现cad拒绝呼叫的情况,有没有像vb6那样可以完美打开CAD的方法?
遇到同样的问题, 请教下楼主咋解决的这个问题 整个过程放在try catch里可以吗? 有可能是交互太快了,增加判断cad空闲试试,CAD忙就等着 使用 MessageFilter.Register(); 来处理“拒绝呼叫”问题:
用com接口要先创建程序域,再跨域调用,
完成调用要释放程序域.
不然com接口会长时间被占用...就造成此类问题
微软解决方案:
重写IOleMessageFilter
方案合集
本论坛同类问题
本论坛链接A
本论坛链接B
页:
[1]