title: Office VSTO AddIn stops responding after invoking Regex several times
body: <p>I am writing a VSTO C# code that would copy the content of active projects in Visual Basic editor in any Office application and process it following some rules. The AddIn adds a button to the VBE toolbar and an event for a click.</p>
  
  <p>When debugging, it works fine the first couple of times (between 8 and 12) I click the button but then it just stops responding to the clicks. Restarting the application does not help, but repeat debugging does (again, for the first 8 - 12 clicks).</p>
  
  <p>I experimented with the code and found that it behaves this way whenever the code uses Regex, even something rather basic. Any ideas what may be going wrong?</p>
  
  <p>This piece of code is for the illustration only. All it is supposed to do is to produce a message box "OK" after it applies Regex to each code module content. And so it does - for the first 8 to 12 times. After that, the button clicks produce no response whatsoever.</p>
  
  <p>No error messages are shown, except when it is building the solution, among many lines it shows <strong>Exception thrown: 'System.Deployment.Application.DeploymentException' in System.Deployment.dll</strong>, which does not seem to affect the program during the first clicks.</p>
  
  <pre><code>using Office = Microsoft.Office.Core;
  using VBA = Microsoft.Vbe.Interop;
  using System.Text.RegularExpressions;
  
  namespace CodeControl
  {
      public partial class ThisAddIn
      {
          private void ThisAddIn_Startup(object sender, System.EventArgs e)
          {
              AddButton();
          }
          public void AddButton()
          {               
              Office.CommandBar cbr = Globals.ThisAddIn.Application.VBE.CommandBars["Edit"];
              Office.CommandBarButton button = cbr.Controls.Add(Type: 1, Temporary: true).Control;
              button.FaceId = 10;
              button.Visible = true;
              Globals.ThisAddIn.Application.VBE.Events.CommandBarEvents[button].Click += 
                  new VBA._dispCommandBarControlEvents_ClickEventHandler(DoWork);
          }
          void DoWork(object CommandBarControl, ref bool handled, ref bool CancelDefault)
          {            
              foreach (VBA.VBComponent component in Globals.ThisAddIn.Application.VBE.ActiveVBProject.VBComponents)
              {
                  VBA.CodeModule module = component.CodeModule;
                  if (module.CountOfLines &lt; 4)
                      continue;
                  string text = module.Lines[1, module.CountOfLines];
                  foreach (string lineText in Regex.Split(text, @"(?:\r\n|\n|\r)"))
                     Console.WriteLine(lineText);
                  System.Windows.Forms.MessageBox.Show(module.Name);
              }            
          }
          private void ThisAddIn_Shutdown(object sender, System.EventArgs e){}
  
          #region VSTO generated code
  
          private void InternalStartup()
          {
              this.Startup += new System.EventHandler(ThisAddIn_Startup);
              this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
          }        
          #endregion
      }
  }
  </code></pre>