gpt4 book ai didi

C# 超时与 Solidworks VBA 宏

转载 作者:行者123 更新时间:2023-11-30 23:29:33 25 4
gpt4 key购买 nike

我在 Solidworks 插件中有几个函数调用 VBA 宏(通过 runMacro2 方法),这是一位同事在过去几周一直在研究的。在他的代码中,他调用了一个 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);
}
}

然后将挂起的代码放在一个 block 中:

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();
}
}
}

关于C# 超时与 Solidworks VBA 宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35343973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com