非托管类方法:非标准语法;使用'&'创建指向成员的指针

时间:2016-11-17 15:55:41

标签: c++ multithreading clr unmanaged

我想在托管的线程中运行一个来自非托管类的方法,并且很快就感到困惑,对CLR是新手。

#include <boost/asio/io_service.hpp>
using namespace System::Threading;

public ref class ManagedClass;

int main()
{
    ManagedClass^ managedObject = gcnew ManagedClass();

    Thread^ threadOk = gcnew Thread(
                         gcnew ThreadStart(
                           managedObject,
                           &ManagedClass::Run));


    boost::asio::io_service unmanagedObject;

    Thread^ threadWrong = gcnew Thread(
                            gcnew ThreadStart(
                              unmanagedObject,
                              &boost::asio::io_service::run));
}

显然,因为

  

无效的委托初始化程序 - 函数不是托管类的成员

所以我开始在Google上搜索非托管代码上的托管线程,但没有发现任何澄清。有什么建议?我完全错了吗?

1 个答案:

答案 0 :(得分:0)

正如UKMonkey所说,一个小样板就足够了......

public ref class CMyStartService
{
    boost::asio::io_service * _pIoService;

public:
    CMyStartService(boost::asio::io_service * pIoService) :
        _pIoService(pIoService)
    {
    }

    void run()
    {
        _pIoService->run();
    }
};

// in main

CMyStartService^ cMyStartService = gcnew CMyStartService(&ioService);
Thread^ thread_io_service = gcnew Thread( 
                              gcnew ThreadStart(
                                cMyStartService, &CMyStartService::run));