none
C++ CLR: CreateThread Error

    Question

  • Ok i was creating basic project starting in "Dev-C++" using following code to create a thread:

    DWORD WINAPI runThread1(LPVOID args)
    {
    while (1 == 1)
    {

    Sleep(1000);
    }

    int main()
    {

    DWORD threadId1;

    HANDLE hThread1 = CreateThread( NULL, 0, runThread1, NULL, 0, &threadId1);

    getchar();

    return 0;
    }
    Which i was using console based application as i was just testing "CreateThread" API..

    When i added it into "Visual C++ CLR" same way it did not work:
    Form1.h(381) : warning C4441: calling convention of '__stdcall ' ignored; '__clrcall ' used instead
    Form1.h(371) : error C3867: 'CHatesPOZ::Form1::find': function call missing argument list; use '&CHatesPOZ::Form1::find' to create a pointer to member
    Also i did try adding:
    #pragma comment(lib, "kernel32.lib")
    see if that would help but no luck..

    If anyone has any suggestions that would be great thanks
    Tuesday, August 11, 2009 10:30 AM

Answers

  • You are mixing unmanaged with managed code.  CreateThread() requires the thread function to be unmanaged code.  That can be done by wrapping your thread function with #pragma managed so it is compiled as unmanaged code.  Or by using a delegate and Marshal::GetFunctionPointerForDelegate().

    However, I'd strongly recommend you use a managed thread instead so you can easily call managed code from it and the CLR can properly manage the thread itself.  Use the System::Threads::Thread class.

    Hans Passant.
    • Marked as answer by Nancy Shao Tuesday, August 18, 2009 6:21 AM
    Tuesday, August 11, 2009 11:00 AM
    Moderator

All replies

  • You are mixing unmanaged with managed code.  CreateThread() requires the thread function to be unmanaged code.  That can be done by wrapping your thread function with #pragma managed so it is compiled as unmanaged code.  Or by using a delegate and Marshal::GetFunctionPointerForDelegate().

    However, I'd strongly recommend you use a managed thread instead so you can easily call managed code from it and the CLR can properly manage the thread itself.  Use the System::Threads::Thread class.

    Hans Passant.
    • Marked as answer by Nancy Shao Tuesday, August 18, 2009 6:21 AM
    Tuesday, August 11, 2009 11:00 AM
    Moderator
  • Thanks for reply NoBugz

    I decided to use the " System::Threads::Thread" but in Visual C++ 2008 Express this dont exisit but i seen a
    similar called "System::Threading" so i'm currently trying to use that

    So when you say im using managed and unmanged code
    Im guessing that:
    HANDLE hThread1 = CreateThread( NULL, 0, runThread1, NULL, 0, &threadId1);
    Is that unmanaged code?

    I don't suppose you could give me example of manged CLR Thread i'm getting real confused between the managed and unmanged code lol >.< If not i'll go do more research on your suggests

    Once again thanks for reply

    Tuesday, August 11, 2009 11:45 AM
  • Right, it is Threading.  Yes, CreateThread() is an unmanaged Windows API function, it doesn't know how to call managed code without help.  You'll find plenty of example C++/CLI code in the MSDN Library article for the Thread class.

    Hans Passant.
    Tuesday, August 11, 2009 11:58 AM
    Moderator