2

I have a few functions in a Solidworks Addin which call on a VBA macro (Via the runMacro2 method) a co-worker has been working on for the last few weeks. In his code he calls a Solidworks function which, under certain, unknown conditions, hangs for a long period of time. How long seems to depend upon the size and quantity of bodies in the part. Considering at least one of the functions we want to run this from i automatic, this just wont do.

I have tried using the Thread.Join(int) method (shown below) but it doesnt work. I also tried modifying the code from this answer Close a MessageBox after several seconds with the same results. Is there anything I can do either in C# or VBA to handle a timeout for this without re-writing his entire macro?

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

1 Answer 1

2

I was getting this same exact issue with SOLIDWORKS hanging on an open of a file. Almost all reference on SO was that you should never do this, but in this scenario, you either have to close it or wait forever. In C# I created a callWithTimeout method:

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

Then the code that was hanging put in a block as such:

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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

14 Comments

I tried this, replacing the "// code that hangs here" with "bbRun();", as well as the actual macro call I show above, neither worked. I assume the timeout you have, 60000 is 60000 ms, or 60 seconds, correct?
yes 60 seconds...can you step through to see what happens? It should call the block of code using the asyncwaithandle.waitone(). You might not need the part to kill the solidworks process in the finally block. that was what i had to do to get it going again
Stepping through, it got to this line if 'if(result.AsyncWaitHandle.WaitOne(timeoutMilliseconds))' before it hung again.
instead of the iSwApp.RunMacro2(...), can you try a Thread.Sleep(60001); see if it kicks out when running code instead of a macro.
The Thread.Sleep(60001) did kick out like it should. I ended up putting the actual line of code which is causing the hang in there swModel.Extension.Create3DBoundingBox() and it still hangs under the same conditions, the timeout is making no difference.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.