_beginthreadex cannot convert from 'overloaded-function'

后端 未结 2 1071
我寻月下人不归
我寻月下人不归 2021-01-26 07:00

So I was making a function to print text layered across a different window, and I wanted it to be in a separate thread so I can run a timer for the text to display while leaving

相关标签:
2条回答
  • 2021-01-26 07:38

    In a nutshell thread startup functions need to follow an exact prototype, and not the one you used.

    They can accept a function that takes a single void *.

    There are several solutions.

    1. Change your function to accept a void*. Right away cast it to a 'struct *' of some type you have created and has the data you want. You would typically create the struct in main with new/malloc, and then delete/free it when you don't need it in the thread function.
    2. The somewhat cleaner alternative is to 'new' up an object of a class you have made. Give that class a public static method that takes said void *. Use the static method as the thread starter, and pass the address of the object as 'this'. Then have the static cast the void * to the object type and call some 'start/run' routine on the object proper. Have the object delete itself before returning from the thread routine unless you have a more coordinated solution across the threads.
    0 讨论(0)
  • 2021-01-26 07:50
    _beginthreadex(0, 0, DrawText,(void *)(hwnd, 175, 15, text, 8), 0 , &threadID);
    

    According to MSDN, third argument must be pointer to a function whose argument is of type void*. In your case, DrawText is a function whose argument is not void* but (HWND hWnd, float x, float y, wchar_t* mybuffer, float DisplayTime). Hence the error and take a look at the examples in the link.

    0 讨论(0)
提交回复
热议问题