In calling CreateTHRead Creating a thread, the third parameter is a function called an address pointer, but it is unable to pass a class's member function as a parameter, I am writing a class for network communication, I hope to be able to make threads Create a package, find it online, find some people do this similar discussion, posted here to share with you! At the same time, I hope that the masters who will see the post will give you your opinion! Q Yunyu Chen: Operating System: Windows NT / 2000 Programming Tools: VC 6.0 Problem: My Questions and QA003733 "Compile Error: 'AfxBeginthread': None of the 2 Overloads CAN Convert Parameter 1 from Type 'Unsigned Int (Void * )'" very similar. But Liao Xiangang did not answer Qa003733 issues. That is, Compiler complains that the first parameter cannot be converted. Answer is the type conversion problem of the second parameter. In fact, if we want to design the following Class: Class CrealTimetask {public: uint task (lpvoid param); Bool StartTask ();}; uint crealtimetask :: taskmain (lpvoid param) {return true;} Bool CrealTimetask :: StartTask ) {AFXBEGINTHREAD (TASKMAIN, NULL);} We will encounter this problem: 'AfxBeginthread': None of the 2 Overloads CAN Convert Parameter 1 from Type 'unsigned int (void *)' Further analysis found: TaskMain's type is : Unsigned int (CREALTIMETASK ::) (void *). So does not match. Although we know that ask, you can find a Walkaround. But if we must implement this function in the class, what should I do? Welcome to this issue of Guru issued by the compilation experience. Level: Master a Answer: This situation, usually declares the thread function as static, such as: Class CREALTIMETASK {public: static uint task (lpvoid param); Bool StartTask ();}; kakusun's view: You define in the class The member function, the VC will impose a THIS pointer when compiling, so what you said will occur. Declaring the member function as a Static type, remove the THIS pointer, but Static member functions can only access Static members. Opinions of Karymay: You can achieve callbacks for member functions, and access non-statist members, as shown below, this is the class I achieved in order to implement the thread function to access class members. It is better than the MFC implementation method.
Class Base; TypeDef Int (Void * P); Struct Callback (Voidback * PFUC; Base * pthis;}; class base {static int mythreadfuc (void * p) {struct callback * P1 = (struct callback *) p; base; fncallback * pfuc = p1-> pfuc; void * param = p1-> param; int i = (PTHIS -> * PFUC) (param); Delete p; return i;} public: voidback pfuc, void * param) {struct callback; p.Param = param; p.PTHIS = THIS; P.PFUC = PFUC; :: CreateThread (MythreadFuc, P); Virtual Int MyCallback (Void * P) {Printf ("IT's Base Class./n"; Return 0;}}; Class Derived: Public Base {Int MyCallback (void * p) {Printf "It's derived class / n");}}; void mycreatethreadimitate (fncallback fuc, void * p) {(* fuc) (p);} void main () {base p; char * param; p.MYCREATTHREAD (&) Base :: mycallback, param; derived p2; p2.mycreatethread (& (Base :: MyCallback), Para m);} // my callback address: karymay@163.net ninjax opinion: In fact, use a static member function to do the thread function is not bad, you can pass the THIS pointer with param when calling the function, then use the type conversion in the thread function Convert to point to this class, then the pointer can access non-static variables and private variables. KEN's opinion: Because the thread function is brought to a friend function, this can be introduced to the pointer of the class, access to the members; Class CrealTimetask {public: Friend uint task (lpvoid param); Bool StartTask ();}; uint taskmain (Lpvoid param) {createtask * ptaskmain = (createtask *) param; // References to PTASKMain pointer. Return True;} BOOL CREALTIMETASK :: StartTask () {AFXBEGINTHREAD (TASKMAIN, THIS);} This problem is replied by XZC. Author Blog:
Http://blog.9 Press .NET / ICE E ZONE /