title: GUI Freezes - Invalid Operation Exception c#
body: <p>I have a GUI in c# where you can open log files.
  The program have 2 main functions:
  setTextMessageTextBox() which updates the GUI when needed (after each operation, giving the appropriate messages)</p>
  
  <pre><code>private static void setTextMessageTextBox(){
    MyProject.mainForm update = new MyProject.mainForm();
  
    //messageTextBox.Text is set by default to "Open the file".
  
    const string msg_0 = "Open the file";
    const string msg_1 = "Press button to start reading";
    const string msg_2 = "Please wait, Reading in progress";
    const string msg_3 = "Reading finished";
  
    string textCurrent = update.messageTextBox.Text;
  
    switch (textCurrent){
      case msg_0 :
        update.messageTextBox.Text = msg_1;
        break;
      case msg_1 :
        update.messageTextBox.Text = msg_2;
        break;
      case msg_2 :
        update.messageTextBox.Text = msg_3;
        break;
      case msg_3 :
        //msg_4 is a global variable with the result
        update.messageTextBox.Text = msg_4;
        break;
    }//switch
  }//method setMessageBoxTest
  </code></pre>
  
  <p>Then, i have the analyze() method which does all the work (the log is very big so it takes up to 1 min to finish).</p>
  
  <pre><code>private void analyze(){
    //reading log
    //cutting log
    //calls other classes to take what pieces are needed
    //calls other classes to decode pieces
    //saves the final result to msg_4
  }
  </code></pre>
  
  <p>Finally I have the button AnalyzeButton which triggers:</p>
  
  <pre><code>private void analyzeButton_Click(object sender, EventsArgs e){
  
    setTextMessageTextBox();
  
    analyze();
  
    setTextMessageTextBox();
  
  }//analyzeButton
  </code></pre>
  
  <p>The result is that the GUI freezes and the textBox is updated only after the analyze function finishes.</p>
  
  <p>I tried:</p>
  
  <pre><code>MethodInvoker startAnalyze = new MethodInvoker(analyze);
  startAnalyze.Invoke();
  </code></pre>
  
  <p>but the GUI still Freezes</p>
  
  <p>I tried to invoke the setTextBox</p>
  
  <pre><code>MethodInvoker setMessage = new MethodInvoker(setMessageTextBox);
  setMessage.Invoke();
  </code></pre>
  
  <p>the GUI still Freezes</p>
  
  <p>I tried to use both with method invoker but the Invalid Operation Exception is thrown.</p>
  
  <p>Finally I used the begin_invoke instead of Invoke to analyze:</p>
  
  <pre><code>MethodInvoker beginning = new MethodInvoker (analyze);
  beginning.BeginInvoke(null, null)
  </code></pre>
  
  <p>The GUI doesn't freeze anymore but still the textBox is not updated.</p>
  
  <p>I have read about BackgroundWorker but maybe I am missing something and it didn't work at all.
  Anybody has a clue of what I am missing or if there is a better way to solve?
  Thank you for your time.</p>
  
  <hr>
  
  <p>Update:
  @SriramSakthivel I moved the creation of the new instance to a global variable level, but the result still the same. The GUI doesnot freeze but the textBox is not updated. It is updated just before I press the button . ex . The text changes from <em>"Open the file"</em> to <em>Press button to start reading</em> but after I press the button the textBox remains as it is.
  Having the new instance as global I tested in 2 ways:
  calling the setMessageTextBox, and assigning the new message manually before and after the appropriate action.
  ex.
  '
        update.messageTextBox.Text = "Analyzing";
        update.messageTextBox.Refresh();
        analyze();
  '
  I tried with and without .Refresh() but still nothing changes.</p>