Pergunta

I am working on managed C++ or C++/CLI. I am trying to start a CLI thread to execute a function. However when I try to build I get the error "Microsoft (R) C/C++ optimizing compiler has stopped working." In the output window. "Foo.cpp(8): fatal error C1001: An internal error has occurred in the compiler."

//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();
}

Is ThreadStart not capable of handling native function pointers? If not, is there is another way to run a regluar C function pointer from C++/CLI?

Foi útil?

Solução

Try substituting line 8 with

ThreadStart^ param = (ThreadStart^) System::Runtime::InteropServices::Marshal::GetDelegateForFunctionPointer((IntPtr)func, ThreadStart::typeid);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top