N
not_a_commie
First, all over my code I have something like this:
if(dumbEvent != null)
dumbEvent(blah);
What's dumb about that is that the compiler or JIT engine should be
able to figure out if the thing is null or not and skip it. Another
thing that's dumb about that is I'm always nervous that somebody will
disconnect the last event in between the "if" and the trigger. Calling
an event should be atomic and the coder should not have to worry about
that.
Second, dealing with WinForms, Control.Invoke (or BeginInvoke) lock on
"this". That's genius, considering Invoke comes in real handy when
you're not dealing with cross-thread issues ;-). No public class
should ever lock on "this". It's just not fair to the coder. Anybody
know if this is fixed with the Dispatcher in WPF?
Another issue with events. According to the documentation, Thread.Join
should continue to process messages. It doesn't. Here is my code to
test this. Maybe somebody could explain my issue here:
using System;
using System.Windows.Forms;
using System.Threading;
namespace StrangeLock
{
internal class Program
{
public class Tester : IDisposable
{
private Form form;
public Tester()
{
form = new Form();
form.Show();
}
public void Enable()
{
if (form.InvokeRequired)
{
form.Invoke(new MethodInvoker(Enable));
return;
}
}
public void Dispose()
{
form.Dispose();
}
public object SyncRoot
{
get { return form; }
}
}
public static Tester tester;
public static void ThreadedEnable()
{
tester.Enable();
}
[STAThread]
private static void Main()
{
tester = new Tester();
Thread thread = new Thread(new ThreadStart(ThreadedEnable));
// lock (tester) //should lock with tester.SyncRoot, but not tester,
and definitely not without a lock here
// {
thread.Start();
thread.Join();
// }
tester.Dispose();
}
}
}
if(dumbEvent != null)
dumbEvent(blah);
What's dumb about that is that the compiler or JIT engine should be
able to figure out if the thing is null or not and skip it. Another
thing that's dumb about that is I'm always nervous that somebody will
disconnect the last event in between the "if" and the trigger. Calling
an event should be atomic and the coder should not have to worry about
that.
Second, dealing with WinForms, Control.Invoke (or BeginInvoke) lock on
"this". That's genius, considering Invoke comes in real handy when
you're not dealing with cross-thread issues ;-). No public class
should ever lock on "this". It's just not fair to the coder. Anybody
know if this is fixed with the Dispatcher in WPF?
Another issue with events. According to the documentation, Thread.Join
should continue to process messages. It doesn't. Here is my code to
test this. Maybe somebody could explain my issue here:
using System;
using System.Windows.Forms;
using System.Threading;
namespace StrangeLock
{
internal class Program
{
public class Tester : IDisposable
{
private Form form;
public Tester()
{
form = new Form();
form.Show();
}
public void Enable()
{
if (form.InvokeRequired)
{
form.Invoke(new MethodInvoker(Enable));
return;
}
}
public void Dispose()
{
form.Dispose();
}
public object SyncRoot
{
get { return form; }
}
}
public static Tester tester;
public static void ThreadedEnable()
{
tester.Enable();
}
[STAThread]
private static void Main()
{
tester = new Tester();
Thread thread = new Thread(new ThreadStart(ThreadedEnable));
// lock (tester) //should lock with tester.SyncRoot, but not tester,
and definitely not without a lock here
// {
thread.Start();
thread.Join();
// }
tester.Dispose();
}
}
}