"Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on." : <p>I'm trying to log some messages to a <code>RichTextBox</code> control.  It logs the first 2 or 3, then throws the following error:</p>

<blockquote>
  <p>"Cross-thread operation not valid: Control 'txtLog' accessed from a thread other than the thread it was created on."</p>
</blockquote>

<p>This is a very simple app that just does a single <a href="/questions/tagged/pubnub" class="post-tag" title="show questions tagged &#39;pubnub&#39;" rel="tag">pubnub</a> subscription.  There was no attempt at threading.  </p>

<p>Per another question I found on SO, I'm using a stringbuilder:</p>

<pre><code>    public StringBuilder logtext = new StringBuilder();
</code></pre>

<p>Then I wanted to simplify things so I could just call <code>log("this is a log message")</code>:</p>

<pre><code>    public void log(string txt)
    {
        logtext.Append(Environment.NewLine + txt);
        txtLog.Text = logtext.ToString();
    }
</code></pre>

<p>Like I said, it logs a few strings fine but then it tries to log the following:</p>

<blockquote>
  <p>"ConnectStatus: [1,\"Connected\",\"presence\"]"</p>
</blockquote>

<p>that's when it throws the error.  Here is the code which is returning that value:</p>

<pre><code>pubnub.Subscribe&lt;string&gt;(
  chnl, 
  DisplayReturnMessage, 
  DisplayConnectStatusMessage, 
  DisplayErrorMessage
);

public void DisplayReturnMessage(string result)
{
    log(TimeStamp() + " Result: " + result);
}
</code></pre>

<p>And here is a ss of the debugger if it helps:</p>

<p><a href="https://i.stack.imgur.com/sKzLh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sKzLh.png" alt="enter image description here"></a></p>

<p>Please ignore the fact that <code>TimeStamp()</code> literally returns "H:mm:ss.ffff" right now :)</p>

<p>I was able to manually <code>log("ConnectStatus: [1,\"Connected\",\"presence\"]")</code> and it worked, so I don't think it's a string issue.  The threading thing is really throwing me off.</p>
