G
Guest
All,
I have a worker thread that fires events across threads to both GUI objects
and thread agnostic objects. My code is working but I want to be assured
that it did it "the right way"... Question: Is there a better way?
According to the .NET docs that I read all that I have to do to fire my
custom "OnSynchronizationStatusChange()" event is:
protected void OnSynchronizationStatusChange(SynchronizationEventArgs e)
{
if (SynchronizationStatusChange != null)
{
SynchronizationStatusChange(this, e);
}
}
This did NOT work across threads - GUI objects received notification on the
worker thread not the GUI thread. Soooo, I reworked it as follows - this
works but I feel that I missed something because I don't feel that I should
have to do this:
protected void OnSynchronizationStatusChange(SynchronizationEventArgs e)
{
if (SynchronizationStatusChange != null)
{
object[] args = new object[2];
args[0] = this;
args[1] = e;
foreach (System.Delegate d in
SynchronizationStatusChange.GetInvocationList())
{
ISynchronizeInvoke isi = d.Target as ISynchronizeInvoke;
if (isi == null)
{
d.DynamicInvoke(args);
}
else
{
isi.BeginInvoke(d, args);
}
}
}
}
Is there a better way to do this??
I have a worker thread that fires events across threads to both GUI objects
and thread agnostic objects. My code is working but I want to be assured
that it did it "the right way"... Question: Is there a better way?
According to the .NET docs that I read all that I have to do to fire my
custom "OnSynchronizationStatusChange()" event is:
protected void OnSynchronizationStatusChange(SynchronizationEventArgs e)
{
if (SynchronizationStatusChange != null)
{
SynchronizationStatusChange(this, e);
}
}
This did NOT work across threads - GUI objects received notification on the
worker thread not the GUI thread. Soooo, I reworked it as follows - this
works but I feel that I missed something because I don't feel that I should
have to do this:
protected void OnSynchronizationStatusChange(SynchronizationEventArgs e)
{
if (SynchronizationStatusChange != null)
{
object[] args = new object[2];
args[0] = this;
args[1] = e;
foreach (System.Delegate d in
SynchronizationStatusChange.GetInvocationList())
{
ISynchronizeInvoke isi = d.Target as ISynchronizeInvoke;
if (isi == null)
{
d.DynamicInvoke(args);
}
else
{
isi.BeginInvoke(d, args);
}
}
}
}
Is there a better way to do this??