Invoke or BeginInvoke usage

M

Meya-awe

Anyone can tell me how to use control.Invoke or .BeginInvoke if i want
to pass some day from a non-ui thread to a ui thread?

Currently, i am using events into the UI module:

private void DataProcessingResults(object o, PresentationEventArgs args)
{
object[] argsData = new object[2];
argsData[0] = o;
argsData[1] = (object) args;
_instance.Invoke(new ResultsDelegate(PresentResults), argsData); // i
get a run-time error here
}

void PresentResults(object o, object[] argsData)
{
// code here maps argsData and extracts results.
}

delegate void PresentResults(object o, object[] argsData);

I get a run time exception on the invoke line from the site
MarshaledInvoke with exception message "Object type cannot be converted
to target type". As you can see, i am not using InvokeRequired property
since i want to bring scope back to the ui thread all together. Also,
_instance is defined as: "private static System.Windows.Forms.Form
_instance and is assigned before the Application.Run line in my code.
what do i need to do to avoid the exception?
thanks,
BRAMOIN
 
J

Jon Skeet [C# MVP]

Meya-awe said:
Anyone can tell me how to use control.Invoke or .BeginInvoke if i want
to pass some day from a non-ui thread to a ui thread?

Currently, i am using events into the UI module:

private void DataProcessingResults(object o, PresentationEventArgs args)
{
object[] argsData = new object[2];
argsData[0] = o;
argsData[1] = (object) args;
_instance.Invoke(new ResultsDelegate(PresentResults), argsData); // i
get a run-time error here
}

Yes - this is because the second parameter to PresentResults is an
object[], but you're passing in a PresentationEventArgs. You could
change your code to:

argsData[1] = new object[]{args};

and I believe it would work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top