Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zhenlinyang/221f1978953e6ff0020306494900d1a9 to your computer and use it in GitHub Desktop.
Save zhenlinyang/221f1978953e6ff0020306494900d1a9 to your computer and use it in GitHub Desktop.
Make Thread-Safe Calls to Windows Forms Controls
public delegate void LogCallback(string str, Color color);
private void LogHandler(string str, Color color)
{
if (LogRichTextBox.InvokeRequired)
{
LogCallback logCallback = LogCallbackFunction;
Invoke(logCallback, new object[] {str, color});
}
else
{
LogCallbackFunction(str, color);
}
}
private void LogCallbackFunction(string str, Color color)
{
LogRichTextBox.SelectionFont = new Font(FontFamily.GenericMonospace, 12, FontStyle.Regular);
LogRichTextBox.SelectionColor = color;
LogRichTextBox.AppendText(str);
LogRichTextBox.AppendText("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment