.NET eventu sūtīšana uz UI thread
Kā ir pareizāk? Kā ir labāk? Kā ir optimālāk?
public class Class
{
delegate void InvokeEvent(EventArgs e);
public event EventHandler Event;
public Class()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(Work));
}
void Work()
{
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new InvokeEvent(OnEvent), new EventArgs());
}
protected void OnEvent(EventArgs e)
{
if (Event != null) Event(this, e);
}
}
vai
public class Class
{
public event EventHandler Event;
public Class()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(Work));
}
void Work(object state)
{
OnEvent(new EventArgs());
}
protected void OnEvent(EventArgs e)
{
if (Event != null)
{
if (Thread.CurrentThread == Application.Current.Dispatcher.Thread)
{
Event(this, e);
}
else
{
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new EventHandler(Event), this, e)
}
}
}
}
?