在C ++ / CLI使用.NET(3.5)任务并行库 [英] Using .NET (3.5) Task Parallel Library in C++/ CLI

查看:616
本文介绍了在C ++ / CLI使用.NET(3.5)任务并行库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我下载无扩展的NET 3.5使用它在Visual Studio 2008中使用C ++ / CLI ...

但是,所有任务并行库的例子是在C#中......我不能够找出即使转换是简单的C#语句转换为C ++ / CLI ...

  //使用一个Action委托和指定的方法
任务任务1 =新的任务(新动作(printMessage));

//使用匿名委托
任务TASK2 =新任务(委托{
printMessage();
});
 

如何用C这些声明++ / CLI?

最良好的祝愿

解决方案

 的#includestdafx.h中
#使用< System.Core.dll的>
使用命名空间系统;
使用命名空间系统:线程::任务;

引用类SomeTask {
上市:
    静态INT的run(){
        返回42;
    }
};

INT主(数组<系统::字符串^> ^参数)
{
    任务< INT> ^ =任务任务< INT> ::厂位于> StartNew(gcnew Func键< INT>(安培; SomeTask ::运行));
    任务 - >等待();
    控制台:的WriteLine(任务 - >结果);
    返回0;
}
 

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

解决方案

#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;
}

这篇关于在C ++ / CLI使用.NET(3.5)任务并行库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆