I'm trying to log some messages to a RichTextBox control. It logs the first 2 or 3, then throws the following error:
"Cross-thread operation not valid: Control 'txtLog' accessed from a thread other than the thread it was created on."
This is a very simple app that just does a single pubnub subscription. There was no attempt at threading.
Per another question I found on SO, I'm using a stringbuilder:
public StringBuilder logtext = new StringBuilder();
Then I wanted to simplify things so I could just call log("this is a log message"):
public void log(string txt)
{
logtext.Append(Environment.NewLine + txt);
txtLog.Text = logtext.ToString();
}
Like I said, it logs a few strings fine but then it tries to log the following:
"ConnectStatus: [1,\"Connected\",\"presence\"]"
that's when it throws the error. Here is the code which is returning that value:
pubnub.Subscribe<string>(
chnl,
DisplayReturnMessage,
DisplayConnectStatusMessage,
DisplayErrorMessage
);
public void DisplayReturnMessage(string result)
{
log(TimeStamp() + " Result: " + result);
}
And here is a ss of the debugger if it helps:
Please ignore the fact that TimeStamp() literally returns "H:mm:ss.ffff" right now :)
I was able to manually log("ConnectStatus: [1,\"Connected\",\"presence\"]") and it worked, so I don't think it's a string issue. The threading thing is really throwing me off.