如何混合并发运行时与.NET代码? [英] How can I mix the Concurrency Runtime with .NET code?

查看:261
本文介绍了如何混合并发运行时与.NET代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中使用并发运行时静态库,最近想在C ++ / CLI项目中使用这个库,以利用Windows窗体设计器并避免MFC。不幸的是,并发运行时与C ++ / CLI中所需的/ clr开关不兼容。我试着在#pragma unmanaged ... #pragma managed指令中使用并发运行时环绕包含的头文件,但是虽然这对我和其他代码在过去工作,在这种情况下似乎不工作。我的意思是我得到的错误:

  C:\Program Files(x86)\Microsoft Visual Studio 10.0 \\ VC\include\concrt.h(27):致命错误C1189:#error:错误:编译/ clr时不支持并发运行时。 

我不是很熟悉混合管理和非托管代码,所以可能有一个工作 - 我不知道的。但另一方面,也许这只是一个愚蠢的方法。如果不是因为我发现MFC不可能复杂,并且Form设计器这么好和容易,我只是做纯C ++。

在C ++ / CLI中使用ConcRT在concrt.h中显式禁用了下面的语句,因为它没有正式支持...

  #if defined(_M_CEE)
#error ERROR:编译/ clr时不支持并发运行时。
#endif

您可以使用PInvoke解决上述问题,也使用指向实现成语的指针通过转发声明一个'pimpl'类并隐藏对concrt.h的依赖到一个本地的.cpp文件来解决这个问题,然后你可以编译成一个lib和链接头文件。 p>

例如在.h文件中:

  //转发声明
class PImpl;

class MyClass
{
....
//向前声明就足够了,因为这是一个指针
PImpl * m_pImpl;
}

在你的.cpp文件中,它编译成一个本地库:

  #include< ppl.h> 
class PImpl
{
//一些concrt类
并发:: task_group m_tasks;
}


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.

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?

解决方案

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

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.

e.g. in the .h file:

//forward declaration
class PImpl;

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

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代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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