Visual C ++ / CLI,带有函数指针的ThreadStart

时间:2013-01-29 15:20:46

标签: multithreading visual-studio-2010 c++-cli managed-c++

我正在使用托管C ++或C ++ / CLI。我正在尝试启动CLI线程来执行一个函数。但是,当我尝试构建时,我收到错误“Microsoft(R)C / C ++优化编译器已停止工作。”在输出窗口中。 “Foo.cpp(8):致命错误C1001:编译器中发生内部错误。”

//the class which holds the function to run 
ref class Foo
{
    void handleEvent();
    void (*func)(void);
};

void Foo::handleEvent()
{
    ThreadStart ^ param = gcnew ThreadStart(func); //line 8
    Thread ^ thread = gcnew Thread(param);
    thread.Start();
}

ThreadStart是否无法处理本机函数指针?如果没有,是否有另一种方法可以从C ++ / CLI运行regluar C函数指针?

1 个答案:

答案 0 :(得分:2)

尝试用第8行代替

ThreadStart^ param = (ThreadStart^) System::Runtime::InteropServices::Marshal::GetDelegateForFunctionPointer((IntPtr)func, ThreadStart::typeid);