I have a Dialog based MFC application. In the event handler of one of my button, I want to create a thread with apartment set as a single threaded apartment. In the launching thread i want to perfoem some operation. I want that application first execute launching thread function and after that it perform other operations written in the handler. I have written a code which is as below:
Now when I execute this then I first get messagebox written as 1 and after that 2. This means that thread function is executing as an asynchronous operation. So my problem is that what should I do so that it first display 2 and then 1.
I have a Dialog based MFC application. In the event handler of one of my button, I want to create a thread with apartment set as a single threaded apartment. In the launching thread i want to perfoem some operation. I want that application first execute launching thread function and after that it perform other operations written in the handler. I have written a code which is as below:
........
Thanks,
Amit
In a MFC application you MUST use AfxBeginThread instead of ::CreateThread function.
See also Using Worker Threads
"Now when I execute this then I first get messagebox written as 1 and after that 2. This means that thread function is executing as an asynchronous operation. So my problem is that what should I do so that it first display 2 and then 1"
Actually I am first getting messagebox 2 and then 1. My requirement is that i should first get messagebox 1 and then 2.
If I call WaitForSingleObject(hThread, INFINITE) after the CreateThread then it is not displaying any messages i.e neither 1 nor 2 and it seems hanged.
Bob,
I think you have tested it in Console Application. If you test it in MFC Application (let's assume on the event handler of a button ) then you may face the same problem.
Now after inserting Wait for Single Object, my code looks like below, but I am not getting any output
Please help, I am still facing the problem. If someone has written any successful test for this scenario then I will appreciate if you attach the sample code also.
Now I am getting compiler error as
"error C2664: 'CreateThread' : cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned long (__stdcall *)(void *)'
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.