Using .NET (3.5) Task Parallel Library in C++/ CLI

2019-07-09 00:20发布

问题:

Well, I download Reactive Extensions for NET 3.5 to use it in visual studio 2008 with c++/cli...

But all Task Parallel Library examples are in C#...I can not able to figure out EVEN converting that simple C# statements into C++/ CLI...

// use an Action delegate and a named method
Task task1 = new Task(new Action(printMessage));

// use a anonymous delegate
Task task2 = new Task(delegate {
printMessage();
});

How can i write those statements in C++/CLI?

Best Wishes

回答1:

#include "stdafx.h"
#using <System.Core.dll>
using namespace System;
using namespace System::Threading::Tasks;

ref class SomeTask {
public:
    static int run() {
        return 42;
    }
};

int main(array<System::String ^> ^args)
{
    Task<int>^ task = Task<int>::Factory->StartNew(gcnew Func<int>(&SomeTask::run));
    task->Wait();
    Console::WriteLine(task->Result);
    return 0;
}