nu2vcpp

We are using Visual Studio 6. I didn't get any relevant results for Asynchronous delegates in google. I tried creating a worker thread using AfxBeginThread and got following error. Is it possible to run functions that return values using a worker thread Please help me. Thank you very much.

DllMain(......)

{.....}

static UINT ctrlfn(LPVOID p);

static UINT ctrlfn(LPVOID p)
{
CApp* me = (CApp *)p;
me->retval = me->ChkFngr(); ///---This function returns an integer value that should be returned to VB application.

return 0;
}


extern "C" int __declspec ( dllexport ) fnDll() //// --- this function is imported and called from the vb app.
{
CApp x;
x.running = true;
AfxBeginThread(ctrlfn);
return x.retval;
}

Error :

D:\MyFolder\example\AutoOnBioDev\AutoOnBioDev.cpp(69) : error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned int (__stdcall *)(void *)'


Where did I go wrong



Re: Visual C++ General how to run a function that returns non-void values on a new thread?

einaros

Even if the thread function returned a value -- where would you pick this value up AfxBeginThread obviously returns immediately (and returns something completely different in any case). One way I can suggest, though, is for you to pass a pointer to a struct to the thread function. Within this function you can keep a value or pointer, and a waitable handle. When your thread function has set the value in this struct, it should signal the wait handle -- thus notifying the first thread that the value is in place. The first thread should then pickup the value.




Re: Visual C++ General how to run a function that returns non-void values on a new thread?

nu2vcpp

Thank you very much. Would you please suggest me any weblink or an article that explains multithreading in VC++ 6 with a sample I understand that I have to use another overload of AfxBeginThread which enables user interface. How do I refer to the main or primary thread to return the value set by the thread I created Thank you very much for your time and explanation.




Re: Visual C++ General how to run a function that returns non-void values on a new thread?

Sarath.

The problem with your code is that you are simply spawning a thread and asking it to put some return value. You can't make sure that the value has been updated to the pointer because these threads are different entities. You should synchronize your thread execution by using Wait functions (WaitForSingleObject etc..). If both of your threads acecssing the same data,, you may need to put some locks to synchronize the access. I've not that much used with MFC thread classes, but withe native CreateThread and other realted functions. I think the error you are getting because of the calling convention problem.

You can see a bunch of articles in codeproject or some other your facorite website. MSDN also has a well documentations on same. I think you learn yourself than we explain about threads and synchronization

http://www.codeproject.com/threads/





Re: Visual C++ General how to run a function that returns non-void values on a new thread?

nu2vcpp

Thank you very much for taking time to help me. I will go through the articles and try them.