0

I have this code that runs in parallel with my Form1 class code. The Global.Launcher turns into a true statement after a button is clicked from my Form class. When I run this code, I'm unable to click the add-in (Where the button is located) on Project (Basically Frozen) due to the infinite loop since I can't access the button. Is there a workaround for this? In short, I do not want to run any code in this class until after I press the button on the add-in form. Thanks in advance.

    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            this.Application.NewProject += new Microsoft.Office.Interop.MSProject._EProjectApp2_NewProjectEventHandler(Application_NewProject);

            void Application_NewProject(Microsoft.Office.Interop.MSProject.Project pj)
            {
                while(Global.launcher == false)
                {
                }
                System.Diagnostics.Debug.WriteLine("Starting to run code: ");
            }

        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • 1
    Your code is running on the UI thread. If you start running a loop on the UI thread, then it will halt all UI stuff until it is done. You'll have to start it in a different thread. – Blake Thingstad May 09 '18 at 19:35
  • Thanks for responding Blake! Can you please explain this to me more? My UI will contain a lot of variables from different buttons/selections on the UI that I'll need when executing my program. Which class do I insert the operation coding? For example, I select a few settings, then click run program on my UI. In my case I'm reading from a Microsoft Project File and storing data in a list for now based off Unique IDs. – Andre Davis May 09 '18 at 19:39
  • I do not have a lot of experience with accessing UI elements from another thread, but I have done it before. See [this](https://stackoverflow.com/a/661662/5890367) answer for an introduction to this. – Blake Thingstad May 09 '18 at 19:52
  • Blake, Do you see any issues with having the button call a method(s) in a separate class? I got it work for a small test, but I don't know if there are any long-term issues with that approach? – Andre Davis May 09 '18 at 20:04
  • There isn't anything wrong with calling a method from another class, but that also doesn't necessarily mean your design is correct. It is hard to say without seeing all of the code. I would assume it is fine unless you have a strong reason to think your design is bad. – Blake Thingstad May 09 '18 at 20:15

0 Answers0