By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,671 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,671 IT Pros & Developers. It's quick & easy.

ISynchronizeInvoke.BeginInvoke and polymorphic parameters

P: n/a
I have a problem when using ISynchronizeInvoke.BeginInvoke with polymorphic
parameters. It seems that if I try to call BeginInvoke and in the array of
parameters place an object that's derived from a base class my method
expects as a parameter, the method gets only an object of the type of the
base class, rather than my full object.

I know this sounds convoluted, so here's an example. My program consists of
an engine and a WindowsForms UI. The engine operates on a separate thread
and reports back to the UI using the main form's ISynchronizeInvoke
interface. When engine performs some operation during which it throws an
exception it calls OnProcessingStageException which in turn fires and event
that will be received by the UI. Here's how it looks.

public event EventHandler ProcessingStageException;

void FireEvent(EventHandler handler, Object sender, EventArgs e) {
if (this.target != null && handler != null) {
this.target.BeginInvoke(handler, new object[] {sender, e});
}
}

private void OnProcessingStageException(object sender, EventArgs e) {
FireEvent(ProcessingStageException, this, (ExceptionEventArgs)e);
}

The main form's method for handling this event is defined as follows:

private void OnProcessingStageException(object sender, EventArgs e) {
MessageBox.Show(
this, string.Format("Error performing operation.\nDetails: {0}",
((ExceptionEventArgs)e).ex), "VideoInspector");
}

The problem is that when I try to cast ((ExceptionEventArgs)e) I get
InvalidCast exception, even though ExceptionEventArgs inherits from
EventArgs.

Is this by desing or did I hit some kind of a bug?

Thanks for your help

Andrew
Nov 15 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.