U
User N
I have a log class with static methods that grab and release a mutex as
required. The log class is designed to be called from both GUI and
thread pool threads, and dump output to a logfile and a RichEditBox.
To give you some idea of the approach I took...
private static RichTextBox rtb;
private delegate void RtbAppendTextHandler(string output);
private static RtbAppendTextHandler RtbAppendText;
Log.Init(RichTextBox richTextBox, string logFile)
{
rtb = richTextBox;
RtbAppendText = new RtbAppendTextHandler(rtb.AppendText);
Log.CreateLogFile(logFile);
}
Log.Write(string msg)
{
mutex.WaitOne();
string output = DateTime.Now.ToString() + " : " + msg;
rtb.Invoke(RtbAppendText, new object[] {output});
if(logToFile)
Log.WriteLogFile(output);
mutex.ReleaseMutex();
}
What appears to be happening is a worker thread is grabbing the mutex
and before the rtb.Invoke call the GUI thread waits on the mutex and
there things lockup. Which I take makes some sense, even though it
isn't what I'd like to happen. What I'm unsure of is how to best resolve
this issue.
The only place I manipulate the RichTextBox is in Log methods which
first wait for the mutex. Based on some things I've read, I got the
impression that that alone isn't enough to assure thread safety and when
one wants to manipulate a control from a worker thread they must do so
via Control.[Begin]Invoke. However, for fun, I tried changing the
rtb.Invoke(...) to an rtb.AppendText(output) and hitting the Log with
threads that write at a pretty good clip. Everything worked OK. I
then commented out the lines that wait/release on the mutex and tried
again and quickly got an exception. This leaves me wondering if the
mutex does in fact provide adequate protection. Comments?
required. The log class is designed to be called from both GUI and
thread pool threads, and dump output to a logfile and a RichEditBox.
To give you some idea of the approach I took...
private static RichTextBox rtb;
private delegate void RtbAppendTextHandler(string output);
private static RtbAppendTextHandler RtbAppendText;
Log.Init(RichTextBox richTextBox, string logFile)
{
rtb = richTextBox;
RtbAppendText = new RtbAppendTextHandler(rtb.AppendText);
Log.CreateLogFile(logFile);
}
Log.Write(string msg)
{
mutex.WaitOne();
string output = DateTime.Now.ToString() + " : " + msg;
rtb.Invoke(RtbAppendText, new object[] {output});
if(logToFile)
Log.WriteLogFile(output);
mutex.ReleaseMutex();
}
What appears to be happening is a worker thread is grabbing the mutex
and before the rtb.Invoke call the GUI thread waits on the mutex and
there things lockup. Which I take makes some sense, even though it
isn't what I'd like to happen. What I'm unsure of is how to best resolve
this issue.
The only place I manipulate the RichTextBox is in Log methods which
first wait for the mutex. Based on some things I've read, I got the
impression that that alone isn't enough to assure thread safety and when
one wants to manipulate a control from a worker thread they must do so
via Control.[Begin]Invoke. However, for fun, I tried changing the
rtb.Invoke(...) to an rtb.AppendText(output) and hitting the Log with
threads that write at a pretty good clip. Everything worked OK. I
then commented out the lines that wait/release on the mutex and tried
again and quickly got an exception. This leaves me wondering if the
mutex does in fact provide adequate protection. Comments?