C# executable freezes

C# executable freezes

Daan_M
Collaborator Collaborator
637 Views
5 Replies
Message 1 of 6

C# executable freezes

Daan_M
Collaborator
Collaborator

Hi,

 

My plug-in for Inventor freezes at some point. it's a windows forms application (WFA) made in MVS. I can't minimize or close the userwindow and it said '(is not responding)'.

 

I can't really figure out what causes it, it seems to work fine untill a certain point, see my comments below;

 

    public partial class InventorInfo : Form
    {
        Inventor.Application _invApp;
        public bool LaunchedApp = false;
        public _Document oDoc;
        public String newctime;
        public String oldctime;
  
        public InventorInfo() {
            InitializeComponent();
        }

        private void BtnClose_Click(object sender, EventArgs e) {
            this.Close();
        }

        void RunProgram(object sender, EventArgs e) {    //LAUNCH BUTTON press
           
            while (LaunchedApp == false) {
                if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text) || string.IsNullOrEmpty(textBox3.Text)) {
                    MessageBox.Show("Launch aborted, Fill in the required fields");
                    break;
//works fine 
                }
                else 
                    Launch_invApp();
//launches the app just fine
                    oDoc = _invApp.Documents.Open(textBox1.Text);
//opens the document just fine
                    MessageBox.Show("Launched succesfully, configurator exe is running...");
//after this point is seems to freeze...
                    LaunchedApp = true;         
            }

            System.Threading.Thread.Sleep(10000);
            oldctime = newctime;
 
            while (LaunchedApp == true) {
//strange thing is it does still run this part of the program even when it sais 'not responding'         
                FileInfo info = new FileInfo(textBox3.Text);
                DateTime ctime = info.LastWriteTime;
                newctime = ctime.ToString();
                
                if (newctime.Equals(oldctime))  {
                    System.Threading.Thread.Sleep(1000);
                }
                else 
                    AddiLogic();
                    oldctime = newctime;
                    System.Threading.Thread.Sleep(4000);
            }
        }

 

0 Likes
638 Views
5 Replies
Replies (5)
Message 2 of 6

JelteDeJong
Mentor
Mentor

It seems that you made an endless loop. (I don't see how this loop will ever stop)

while (LaunchedApp == true) {
   System.Threading.Thread.Sleep(1000);
   System.Threading.Thread.Sleep(4000);
}

this loop block the thread and will make the program seem to freeze up.

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 6

Daan_M
Collaborator
Collaborator

Hi Jelte,

 

I'm no programmer but I needed the program to be running at all times, thats why i made a endless loop to repeat (as long as inventor is running) it repeats that block of code.

 

I need the code inside the loop to do a check every so many seconds to see if any parameters changed, if they did, it calls another sub (the AddiLogic()).

 

Or am i interperting it wrong? And does the loop condition conflict with the threading?

if so, how do i get it to do the same result with a different code, without freezing up?

 

Thanks alot

 

 

0 Likes
Message 4 of 6

JhoelForshav
Mentor
Mentor

Hi @Daan_M 

I think you should read up on eventhandlers. What you want to do can be accomplished with ModelingEvents.OnParameterChanged.

 

https://help.autodesk.com/view/INVNTOR/2020/ENU/?guid=GUID-73EF8AD4-6031-42EB-994E-795284DBDEB1

 

 

 

0 Likes
Message 5 of 6

Daan_M
Collaborator
Collaborator

Hi @JhoelForshav, thanks, I read up onthe event handlers and loops inside them. Luckily I wasn't the only one who ran into this problem. I found out the application 'froze' because the infinite while loop inside the eventhandler locked the UI. I placed the loop in a thread outside of the event handler and now it runs fine.

 

Edit; I tried using 'ModelingEvents.OnParameterChanged.' but this seems to only work on files linked/embedded in Inventor, they also have to be actual parameters in the Inventor file, atleast, that was my conclusion. 

 

The Excelfile i use is quite large and contains many parameters that are not in the Inventor model itself, just a few are used to feed the inventor model. Thats why i decided to use a seperate application to trigger the events in Inventor.

0 Likes
Message 6 of 6

JhoelForshav
Mentor
Mentor

Oh, sorry... I thought you wanted to trigger a sub on inventor parameter change. I misunderstood, my bad 🙂

0 Likes