G
Guest
Does anyone have example code that demonstrates how to fire an event on the
main thread of a GUI app from a thread within a component (not control)?
I'm sure that Control.Invoke is required, but do not know how to obtain the
control reference from the component.
In the full framework, this can be obtained from Delegate.Target. VS2005
documentation indicates that Delegate.Target is supported by Compact
Framework, but compiler reports "error CS0117: 'System.Delegate' does not
contain a definition for 'Target'"
This code works for full framework but not compact framwork.
void InvokeDelegate(Delegate del, params object[] args)
{
if (del == null)
return;
ISynchronizeInvoke synchronizer = del.Target as ISynchronizeInvoke;
// Test for GUI application
if ((synchronizer == null) || (!synchronizer.InvokeRequired))
{
// Not a GUI application
try
{
del.DynamicInvoke(args);
}
catch(Exception e)
{
Debug.Assert(false, "del.DynamicInvoke failed: " + e.Message);
}
return;
}
try
{
// GUI application
synchronizer.Invoke(del, args);
}
catch(Exception e)
{
Debug.Assert(false, "synchronizer.Invoke failed: " + e.Message);
}
}
main thread of a GUI app from a thread within a component (not control)?
I'm sure that Control.Invoke is required, but do not know how to obtain the
control reference from the component.
In the full framework, this can be obtained from Delegate.Target. VS2005
documentation indicates that Delegate.Target is supported by Compact
Framework, but compiler reports "error CS0117: 'System.Delegate' does not
contain a definition for 'Target'"
This code works for full framework but not compact framwork.
void InvokeDelegate(Delegate del, params object[] args)
{
if (del == null)
return;
ISynchronizeInvoke synchronizer = del.Target as ISynchronizeInvoke;
// Test for GUI application
if ((synchronizer == null) || (!synchronizer.InvokeRequired))
{
// Not a GUI application
try
{
del.DynamicInvoke(args);
}
catch(Exception e)
{
Debug.Assert(false, "del.DynamicInvoke failed: " + e.Message);
}
return;
}
try
{
// GUI application
synchronizer.Invoke(del, args);
}
catch(Exception e)
{
Debug.Assert(false, "synchronizer.Invoke failed: " + e.Message);
}
}