473,320 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

UI Threads in C++/CLI

Hello

To get an understanding of UI threads in C++/CLI, I attempted to
duplicate the following C# example:

http://msdn.microsoft.com/library/de...ms06112002.asp

Here is the code:

public ref class Form1 : public System::Windows::Forms::Form
{
...
....
....
....
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e)
{
CalcPiDelegate ^calcPi = gcnew CalcPiDelegate(this, &Form1::calcPi);
calcPi->BeginInvoke(1, nullptr, nullptr);

}

private: delegate System::Void CalcPiDelegate(int max);

private: System::Void calcPi(int max)
{
ShowProgress(0, max);

for (int i = 0; i < max; i++)
{
ShowProgress(i, max);
}
}

private: delegate System::Void ShowProgressDelegate(int i, int max);

private: System::Void ShowProgress(int i, int max)
{
if (richTextBox1->InvokeRequired == false)
{

richTextBox1->AppendText(Convert::String(i) +
Environment::NewLine);
progressBar1->Maximum = max;
progressBar1->Value = i;
}
else
{
ShowProgressDelegate ^progDelegate = gcnew
ShowProgressDelegate(this,&Form1::ShowProgress);

cli::array<int> ^args = gcnew cli::array<int>(2);
args[0] = i;
args[1]= max;

BeginInvoke(progDelegate,args);
}
}
};

The BeginInvoke call causes the following exception:

An unhandled exception of type
'System.Reflection.TargetParameterCountException' occurred in mscorlib.dll

Additional information: Parameter count mismatch.

Unfortunately I can not determine why this error occurs. Any help or
hint is greatly appreciated.

Regards

Nov 17 '05 #1
2 7708
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
"None" <--@nowhre.inv> wrote in message
news:3s*********************@fe04.news.easynews.co m... Hello

To get an understanding of UI threads in C++/CLI, I attempted to duplicate
the following C# example:

http://msdn.microsoft.com/library/de...ms06112002.asp

Here is the code:

public ref class Form1 : public System::Windows::Forms::Form
{
..
...
...
...
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e)
{
CalcPiDelegate ^calcPi = gcnew CalcPiDelegate(this, &Form1::calcPi);
calcPi->BeginInvoke(1, nullptr, nullptr);

}

private: delegate System::Void CalcPiDelegate(int max);

private: System::Void calcPi(int max)
{
ShowProgress(0, max);

for (int i = 0; i < max; i++)
{
ShowProgress(i, max);
}
}

private: delegate System::Void ShowProgressDelegate(int i, int max);

private: System::Void ShowProgress(int i, int max)
{
if (richTextBox1->InvokeRequired == false)
{

richTextBox1->AppendText(Convert::String(i) +
Environment::NewLine);
progressBar1->Maximum = max;
progressBar1->Value = i;
}
else
{
ShowProgressDelegate ^progDelegate = gcnew
ShowProgressDelegate(this,&Form1::ShowProgress);

cli::array<int> ^args = gcnew cli::array<int>(2);
args[0] = i;
args[1]= max;

BeginInvoke(progDelegate,args);
}
}
};

The BeginInvoke call causes the following exception:

An unhandled exception of type
'System.Reflection.TargetParameterCountException' occurred in mscorlib.dll

Additional information: Parameter count mismatch.

Unfortunately I can not determine why this error occurs. Any help or hint
is greatly appreciated.

Regards

Nov 17 '05 #2
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

"Marcus Heege" <NO****@heege.net> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
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
"None" <--@nowhre.inv> wrote in message
news:3s*********************@fe04.news.easynews.co m...
Hello

To get an understanding of UI threads in C++/CLI, I attempted to
duplicate the following C# example:

http://msdn.microsoft.com/library/de...ms06112002.asp

Here is the code:

public ref class Form1 : public System::Windows::Forms::Form
{
..
...
...
...
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e)
{
CalcPiDelegate ^calcPi = gcnew CalcPiDelegate(this, &Form1::calcPi);
calcPi->BeginInvoke(1, nullptr, nullptr);

}

private: delegate System::Void CalcPiDelegate(int max);

private: System::Void calcPi(int max)
{
ShowProgress(0, max);

for (int i = 0; i < max; i++)
{
ShowProgress(i, max);
}
}

private: delegate System::Void ShowProgressDelegate(int i, int max);

private: System::Void ShowProgress(int i, int max)
{
if (richTextBox1->InvokeRequired == false)
{

richTextBox1->AppendText(Convert::String(i) +
Environment::NewLine);
progressBar1->Maximum = max;
progressBar1->Value = i;
}
else
{
ShowProgressDelegate ^progDelegate = gcnew
ShowProgressDelegate(this,&Form1::ShowProgress);

cli::array<int> ^args = gcnew cli::array<int>(2);
args[0] = i;
args[1]= max;

BeginInvoke(progDelegate,args);
}
}
};

The BeginInvoke call causes the following exception:

An unhandled exception of type
'System.Reflection.TargetParameterCountException' occurred in
mscorlib.dll

Additional information: Parameter count mismatch.

Unfortunately I can not determine why this error occurs. Any help or hint
is greatly appreciated.

Regards


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ronan Viernes | last post by:
Hi, I have created a python script (see below) to count the maximum number of threads per process (by starting new threads continuously until it breaks). ###### #testThread.py import...
0
by: Al Tobey | last post by:
I was building perl 5.8.2 on RedHat Enterprise Linux 3.0 (AS) today and noticed that it included in it's ccflags "-DTHREADS_HAVE_PIDS." I am building with -Dusethreads. With newer Linux...
6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
34
by: Kovan Akrei | last post by:
Hi, I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program: using System; using System.Threading; using System.Collections; ...
3
by: bygandhi | last post by:
Hi - I am writing a service which will check a process and its threads for their state ( alive or dead ). The process has 5 .net managed threads created using thread.start and each have been...
10
by: [Yosi] | last post by:
I would like to know how threads behavior in .NET . When an application create 4 threads for example start all of them, the OS task manager will execute all 4 thread in deterministic order manes,...
6
by: RahimAsif | last post by:
Hi guys, I would like some advice on thread programming using C#. I am writing an application that communicates with a panel over ethernet, collects data and writes it to a file. The way the...
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
10
by: Darian | last post by:
Is there a way to find all the thread names that are running in a project? For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and T5 are running...I want to be able to know that...
4
by: tdahsu | last post by:
All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.