Solidworks VBA 宏的 C# 超时
C# Timeout with Solidworks VBA Macro
我在 Solidworks 插件中有几个函数调用 VBA 宏(通过 运行Macro2 方法)一位同事在过去几周一直在研究。在他的代码中,他调用了一个 Solidworks 函数,该函数在某些未知条件下会挂起很长时间。多长时间似乎取决于零件中物体的大小和数量。考虑到我们希望从自动 运行 中至少实现一个功能,这是行不通的。
我已经尝试使用 Thread.Join(int) 方法(如下所示),但它不起作用。我还尝试修改此答案 Close a MessageBox after several seconds 中的代码,结果相同。有什么我可以在 C# 或 VBA 中做的事情来处理超时而不用重写他的整个宏吗?
public void runBB()
{
Stopwatch testStop = new Stopwatch();
Thread workerThread = new Thread(bbRun);
testStop.Start();
workerThread.Start();
if (!workerThread.Join(50))
{
workerThread.Abort();
testStop.Stop();
MessageBox.Show("Unable to generate Bounding Box after " + testStop.ElapsedMilliseconds/1000 + " seconds. Please enter data manually.", "Solidworks Derped Error.");
}
return;
}//Still uses Macro (2-5-16)
public static void bbRun()
{
iSwApp.RunMacro2(macroPath + "BOUNDING_BOX.swp", "test11", "main", 0, out runMacroError);
return;
}
我遇到了与 SOLIDWORKS 在打开文件时挂起的完全相同的问题。几乎所有关于 SO 的参考都是你永远不应该这样做,但在这种情况下,你要么必须关闭它,要么永远等待。在 C# 中,我创建了一个 callWithTimeout 方法:
private void callWithTimeout(Action action, int timeoutMilliseconds, String errorText) {
Thread threadToKill = null;
Action wrappedAction = () =>
{
threadToKill = Thread.CurrentThread;
action();
};
IAsyncResult result = wrappedAction.BeginInvoke(null, null);
if (result.AsyncWaitHandle.WaitOne(timeoutMilliseconds)) {
wrappedAction.EndInvoke(result);
} else {
threadToKill.Abort();
throw new TimeoutException(errorText);
}
}
然后将挂起的代码放在一个块中:
bool timedOut = false;
try {
callWithTimeout(delegate() {
// code that hangs here
}, 60000, "Operation timed out. SOLIDWORKS could not open the file. This file will be processed later.");
} catch (TimeoutException){
timedOut = true;
} finally {
if(timedOut) {
Process[] prs = Process.GetProcesses();
foreach (Process p in prs) {
if (p?.ProcessName.Equals("SLDWORKS") ?? false)
p?.Kill();
}
}
}
我在 Solidworks 插件中有几个函数调用 VBA 宏(通过 运行Macro2 方法)一位同事在过去几周一直在研究。在他的代码中,他调用了一个 Solidworks 函数,该函数在某些未知条件下会挂起很长时间。多长时间似乎取决于零件中物体的大小和数量。考虑到我们希望从自动 运行 中至少实现一个功能,这是行不通的。
我已经尝试使用 Thread.Join(int) 方法(如下所示),但它不起作用。我还尝试修改此答案 Close a MessageBox after several seconds 中的代码,结果相同。有什么我可以在 C# 或 VBA 中做的事情来处理超时而不用重写他的整个宏吗?
public void runBB()
{
Stopwatch testStop = new Stopwatch();
Thread workerThread = new Thread(bbRun);
testStop.Start();
workerThread.Start();
if (!workerThread.Join(50))
{
workerThread.Abort();
testStop.Stop();
MessageBox.Show("Unable to generate Bounding Box after " + testStop.ElapsedMilliseconds/1000 + " seconds. Please enter data manually.", "Solidworks Derped Error.");
}
return;
}//Still uses Macro (2-5-16)
public static void bbRun()
{
iSwApp.RunMacro2(macroPath + "BOUNDING_BOX.swp", "test11", "main", 0, out runMacroError);
return;
}
我遇到了与 SOLIDWORKS 在打开文件时挂起的完全相同的问题。几乎所有关于 SO 的参考都是你永远不应该这样做,但在这种情况下,你要么必须关闭它,要么永远等待。在 C# 中,我创建了一个 callWithTimeout 方法:
private void callWithTimeout(Action action, int timeoutMilliseconds, String errorText) {
Thread threadToKill = null;
Action wrappedAction = () =>
{
threadToKill = Thread.CurrentThread;
action();
};
IAsyncResult result = wrappedAction.BeginInvoke(null, null);
if (result.AsyncWaitHandle.WaitOne(timeoutMilliseconds)) {
wrappedAction.EndInvoke(result);
} else {
threadToKill.Abort();
throw new TimeoutException(errorText);
}
}
然后将挂起的代码放在一个块中:
bool timedOut = false;
try {
callWithTimeout(delegate() {
// code that hangs here
}, 60000, "Operation timed out. SOLIDWORKS could not open the file. This file will be processed later.");
} catch (TimeoutException){
timedOut = true;
} finally {
if(timedOut) {
Process[] prs = Process.GetProcesses();
foreach (Process p in prs) {
if (p?.ProcessName.Equals("SLDWORKS") ?? false)
p?.Kill();
}
}
}