[Solved] I hope there is a Managed C++/CLR person left to help me with Parallel::For
-
Microsoft Documentation allows me to change the pull down to C++ from C# but continues to shows C# syntax
#pragma once
namespace CppTutorial {
using namespace System;
using namespace System::Threading::Tasks;
/// /// Summary for MainForm /// public ref class MainForm : public System::Windows::Forms::Form { public: MainForm(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// /// Clean up any resources being used. /// ~MainForm() { if (components) { delete components; } } private: /// /// Required designer variable. /// System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// /// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
void InitializeComponent(void)
{
this->SuspendLayout();
//
// MainForm
//
this->AutoScaleDimensions=System::Drawing::SizeF(6,13);
this->AutoScaleMode=System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize=System::Drawing::Size(284,261);
this->Name=L"MainForm";
this->Text=L"MainForm";
this->Shown+=gcnew System::EventHandler(this,&MainForm::MainForm_Shown);
this->ResumeLayout(false);}
#pragma endregion
public:
static void Body(int index)
{
int test=0;
}delegate void MyDelegate(int index);
System::Void MainForm_Shown(System::Object^ sender,System::EventArgs^ e)
{
MyDelegate ^body=gcnew MyDelegate(MainForm::Body);
ParallelLoopResult ^result=Parallel::For(0,10,(System::Action ^)body);
}};
}I know its all a bit bodged but its the only way i could get it to compile and link but the problem is obvious run time crash System.InvalidCastException: Unable to cast object of type 'MyDelegate' to type 'System.Action`1[System.Int32]'. in MC++/CLI i cant find the right syntax to use for ParallelLoopResult ^result=Parallel::For( without casting to Action there is no generic delegate in the overides Is there any MC++/CLI people left out there that can help or offer any other way to paralleize a for loop i tried the old parallel_for(x,y,z){ "do stuff here" } way but i couldnt get that very far with that either i prefer to stick with .net but this delegate stuff omg :¬
-
Microsoft Documentation allows me to change the pull down to C++ from C# but continues to shows C# syntax
#pragma once
namespace CppTutorial {
using namespace System;
using namespace System::Threading::Tasks;
/// /// Summary for MainForm /// public ref class MainForm : public System::Windows::Forms::Form { public: MainForm(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// /// Clean up any resources being used. /// ~MainForm() { if (components) { delete components; } } private: /// /// Required designer variable. /// System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// /// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
void InitializeComponent(void)
{
this->SuspendLayout();
//
// MainForm
//
this->AutoScaleDimensions=System::Drawing::SizeF(6,13);
this->AutoScaleMode=System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize=System::Drawing::Size(284,261);
this->Name=L"MainForm";
this->Text=L"MainForm";
this->Shown+=gcnew System::EventHandler(this,&MainForm::MainForm_Shown);
this->ResumeLayout(false);}
#pragma endregion
public:
static void Body(int index)
{
int test=0;
}delegate void MyDelegate(int index);
System::Void MainForm_Shown(System::Object^ sender,System::EventArgs^ e)
{
MyDelegate ^body=gcnew MyDelegate(MainForm::Body);
ParallelLoopResult ^result=Parallel::For(0,10,(System::Action ^)body);
}};
}I know its all a bit bodged but its the only way i could get it to compile and link but the problem is obvious run time crash System.InvalidCastException: Unable to cast object of type 'MyDelegate' to type 'System.Action`1[System.Int32]'. in MC++/CLI i cant find the right syntax to use for ParallelLoopResult ^result=Parallel::For( without casting to Action there is no generic delegate in the overides Is there any MC++/CLI people left out there that can help or offer any other way to paralleize a for loop i tried the old parallel_for(x,y,z){ "do stuff here" } way but i couldnt get that very far with that either i prefer to stick with .net but this delegate stuff omg :¬
As the error says, you can't create a delegate of one type, and then cast it to a different type. Change your code to create the correct delegate type in the first place, and remove the cast.
System::Action <int> ^body = gcnew System::Action <int>(MainForm::Body);
ParallelLoopResult ^result = Parallel::For(0, 10, body);
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
As the error says, you can't create a delegate of one type, and then cast it to a different type. Change your code to create the correct delegate type in the first place, and remove the cast.
System::Action <int> ^body = gcnew System::Action <int>(MainForm::Body);
ParallelLoopResult ^result = Parallel::For(0, 10, body);
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thank you very much Richard If MS docs had C++ version of this i would have not neded to bother you I was very confused as i dont do delegates if i can avoid it Very happy some C++/CLR people still exist ill mark as solved Here is the Final working MainForm() showing cross thread update of textBox, Random, and usage of other Member funtions This is here to help anyone that is as bad at this overcomplicated Parallel coding in C++/CLR (my opinion should be easier) "Parallel::For" - intellisense still moans about 2 or more overloads of it but who cares anyway, no one :¬)
namespace CppTutorial {
using namespace System; using namespace System::Threading::Tasks; using namespace System::Windows::Forms; /// /// Summary for MainForm /// public ref class MainForm : public System::Windows::Forms::Form { public: MainForm(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// /// Clean up any resources being used. /// ~MainForm() { if (components) { delete components; } } private: System::Windows::Forms::TextBox ^textBox1; protected: private: /// /// Required designer variable. /// System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// /// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
void InitializeComponent(void)
{
this->textBox1=(gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// textBox1
//
this->textBox1->Dock=System::Windows::Forms::DockStyle::Fill;
this->textBox1->Location=System::Drawing::Point(0,0);
this->textBox1->Multiline=true;
this->textBox1->Name=L"textBox1";
this->textBox1->Size=System::Drawing::Size(563,451);
this->textBox1->TabIndex=0;
//
// MainForm
//
this->AutoScaleDimensions=System::Drawing::SizeF(6,13);
this->AutoScaleMode=System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize=System::Drawing::Size(563,451);
this->Controls->Add(this->textBox1);
this->Name=L"MainForm";
this->Text=L"MainForm";
this->Shown+=gcnew System::EventHandler(this,&MainForm::MainForm_Shown);
this->ResumeLayout(false);
this->PerformLayout();}
#pragma endregion
public:
static MainForm ^staticmain;
Random ^rand1