About a year back I posted a question and eventually implemented the
following code in an event handler as a result:
object[] some_args = new object[1];
some_args[0] = my_event_args.r esults;
ISynchronizeInv oke lSync = this as ISynchronizeInv oke;
if(lSync == null)
{// UI Thread
UIDelegate.Dyna micInvoke(some_ args);
}
else
{// Worker thread
this.Invoke(UID elegate, some_args);
}
Reading the code now I'm questioning it. Can lSync ever be null if the code
is in a WinForm derived class. Other examples I am seeing all do a check of
InvokeRequired before calling Invoke. Since I am in the form code lSync can
never be null, correct? Therefore, in my current code, I am always calling
this.Invoke. I have changed the code to check InvokeRequiredb efore calling
Invoke. If InvokeRequired is false I just make the call directly.
My new code:
object[] some_args = new object[1];
some_args[0] = my_event_args.r esults;
ISynchronizeInv oke lSync = this as ISynchronizeInv oke;
if(lSync.Invoke Required)
{// Worker thread
lSync.Invoke(UI Delegate, some_args);
}
else
{//gui thread
MyHandler(my_ar gs.results);
}
Is this correct? This just seems to have less overhead than always calling
invoke.
Thanks,
jim
following code in an event handler as a result:
object[] some_args = new object[1];
some_args[0] = my_event_args.r esults;
ISynchronizeInv oke lSync = this as ISynchronizeInv oke;
if(lSync == null)
{// UI Thread
UIDelegate.Dyna micInvoke(some_ args);
}
else
{// Worker thread
this.Invoke(UID elegate, some_args);
}
Reading the code now I'm questioning it. Can lSync ever be null if the code
is in a WinForm derived class. Other examples I am seeing all do a check of
InvokeRequired before calling Invoke. Since I am in the form code lSync can
never be null, correct? Therefore, in my current code, I am always calling
this.Invoke. I have changed the code to check InvokeRequiredb efore calling
Invoke. If InvokeRequired is false I just make the call directly.
My new code:
object[] some_args = new object[1];
some_args[0] = my_event_args.r esults;
ISynchronizeInv oke lSync = this as ISynchronizeInv oke;
if(lSync.Invoke Required)
{// Worker thread
lSync.Invoke(UI Delegate, some_args);
}
else
{//gui thread
MyHandler(my_ar gs.results);
}
Is this correct? This just seems to have less overhead than always calling
invoke.
Thanks,
jim
Comment