When I first saw your code, I thought you were kidding me, because IMO this
code should not even compile.
The second argument of Control.BeginInvoke is of type cli::array<Object^>^.
Here is what you pass:
> cli::array<int> ^args = gcnew cli::array<int>(2);
> args[0] = i;
> args[1]= max;
>
> BeginInvoke(progDelegate,args);
Notice that cli::array<int> can *not* be casted into a cli::array<Object^>.
If you call
BeginInvoke(progDelegate, gcnew array<Object^>{i, max});
instead of your call, the app will work.
However, there is still an unanswered question:
The code below tries the same, but he doesn't compile because of the invalid
cast:
using namespace System;
void GetArray(cli::array<Object^>^ arrObj)
{
for each (Object^ o in arrObj)
Console::WriteLine(arrObj);
}
int main()
{
GetArray(gcnew array<Object^>{1, 2});
GetArray(gcnew array<int>{1, 2});
}
In contrast to my code, your code (which does the same cast)
a) it compiles and
b) generates code that produces the wrong array with the cast.
My assumtion is, that control.BeginInvoke is handled specially (and wrong).
Marcus
> Hello
>
[quoted text clipped - 68 lines]
>
> Regards
Marcus Heege - 10 Jun 2005 10:20 GMT
I have just got a response from the team. There is a simple reason for this
behaviour:
Control::BeginInvoke has a ParamArray. Since array<int> cannot be casted
into array<Object^>, they create a new array<Object^> with one element, a
reference to the array<int> and pass this to the function.
Marcus
> When I first saw your code, I thought you were kidding me, because IMO
> this code should not even compile.
[quoted text clipped - 116 lines]
>>
>> Regards