我怎么可以混合并发运行时使用.NET code? [英] How can I mix the Concurrency Runtime with .NET code?

查看:765
本文介绍了我怎么可以混合并发运行时使用.NET code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用并发运行时在C ++静态图书馆,最近想使用这个库在C ++ / CLI的项目,以充分利用Windows窗体设计器,避免MFC。不幸的是,并发运行时不与C ++ / CLI所需/ clr开关兼容。我试着周围使用并发运行时中所包含的头文件的#pragma非托管...的#pragma管理的指令,但同时这对我来说与其他code在过去的工作,它似乎并没有工作在这种情况下。我的意思是,我得到了错误:

I've been using the Concurrency Runtime in a C++ static library, and recently wanted to use this library in a C++/CLI project, to take advantage of the Windows Form designer and avoid MFC. Unfortunately, the Concurrency Runtime is not compatible with the /clr switch required in C++/CLI. I tried surrounding the included header files that use the Concurrency Runtime in the "#pragma unmanaged ... #pragma managed" directives, but while that's worked for me with other code in the past, it doesn't seem to work in this case. By which I mean that I get the error:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\concrt.h(27): fatal error C1189: #error :  ERROR: Concurrency Runtime is not supported when compiling /clr.

我不是超级精通混合托管和非托管code,所以它可能是有一个变通办法,我不知道。但在另一方面,也许这只是一个愚蠢的做法。如果它是不是事实,我觉得MFC不可能复杂,窗体设计如此漂亮和容易的,我只是做纯C ++。随着preference以混合两种,有什么建​​议?

I'm not super well versed in mixing managed and unmanaged code, so it's possible that there's a work-around that I'm not aware of. But on the other hand, perhaps this is just a silly approach. If it weren't for the fact that I find MFC impossibly complex, and the Form designer so nice and easy, I'd just do pure C++. With a preference to mixing the two, any suggestions?

推荐答案

在C ++ / CLI使用ConcRT明确地通过,因为它不是官方支持下面的语句禁用concrt.h ...

Using ConcRT in C++/CLI is explicitly disabled in concrt.h via the statement below because it is not officially supported...

#if defined(_M_CEE)
   #error ERROR: Concurrency Runtime is not supported when compiling /clr.
#endif

您可以使用的PInvoke来解决这个按照上面的建议,或者你也可以使用指针来实现成语用着宣告'平普尔'类来解决这个问题和隐藏concrt.h的依赖到本地.cpp文件然后您可以编译成一个lib和链路反对的头文件。

You can use PInvoke to work around this as suggested above, or you can also use the pointer to implementation idiom to address this by forward declaring a 'pimpl' class and hide the dependency on concrt.h to a native .cpp file which you can then compile into a lib and link against with the header file.

例如。在.h文件中:

//forward declaration
class PImpl;

class MyClass
{
  ....
  //forward declaration is sufficient because this is a pointer
  PImpl* m_pImpl;
}

例如。在您的.cpp文件,该文件编译成一个本地库:

e.g. in your .cpp file which compiles into a native lib:

  #include <ppl.h>
  class PImpl
  {
   //some concrt class
   Concurrency::task_group m_tasks;
  }

这篇关于我怎么可以混合并发运行时使用.NET code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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