Click here to Skip to main content
12,949,826 members (89,736 online)
Rate this:
Please Sign up or sign in to vote.
Hi All,
Why my QueueUserWorkItem is not at all working. My code is here:

typedef struct
    PCHAR DestinationPath ;
int main(int argc, char * argv[])
	BOOL bQuwi ;
	DWORD dwIndex ;
        pData = (PMAINDATA)malloc(sizeof(MAINDATA)) ;
	pData->DestinationPath = NULL ;
	pData->URL[0] = 0 ;
		if(argc < 3)
			printf("Main Error-> Not Enough Arguments\n") ;
			break ;
		printf("START\n") ;
		pData->DestinationPath = argv[2] ;
		while((pURL = (PCHAR)GetNext_FileList(pContext)) != NULL)
			TempURL = StrDupA(pURL) ;
			pData->URL[dwIndex] = (PCHAR)TempURL ;
			dwIndex++ ;
		dwTotalNumberOfURL = dwIndex ;
		bQuwi = QueueUserWorkItem(MyThreadProc,(PVOID) pData,      
                                                             WT_EXECUTEDEFAULT) ;
		if ( !bQuwi )
			printf("Main: QueueUserWorkItem Failed\n");
		printf("SUCCESS\n") ;
	}while(FALSE) ;
        return 0 ;
DWORD CALLBACK MyThreadProc( LPVOID lpParam ) 
    PMAINDATA pData ;
	pData = (PMAINDATA)lpParam ;
	DownloadFile(pData->URL, pData->DestinationPath) ;
    return 0 ; 

I have not submitted the whole code. So please bypass the declaration and free memory errors. The only problem I am facing is that QUWI is not calling my "MyThreadProc" function although it is returning TRUE.
Posted 16-Feb-12 19:48pm
Updated 16-Feb-12 22:36pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

The problem with QueueUserWorkItem is most likely that your main() exits before the worker thread gets a chance to run. Try adding a long Sleep() to your main() after the QueueUserWorkItem() call, or better, synchronize the worker with main(). E.g. something like this:

#include <windows.h>
#include <stdio.h>
// The synchronization object.
// Usually, this would be passed to the worker thread as part of
// context data but to simplify the example I put it here.
HANDLE g_syncEventHandle = 0;
DWORD CALLBACK MyThreadProc( LPVOID lpParam );
int main(int argc, char * argv[])
  g_syncEventHandle = CreateEvent(0, TRUE, FALSE, 0);
  BOOL b = QueueUserWorkItem(MyThreadProc, 0, WT_EXECUTEDEFAULT);
  if ( !b )
    printf("Error: QueueUserWorkItem Failed\n");
    printf("SUCCESS - user work item queued\n") ;
  // Go and do other things here
  // Now the main thread is ready to terminate.
  // Before doing so, check that the worker is done.
  WaitForSingleObject(g_syncEventHandle, INFINITE);
  // And remember to clean up
  printf("main() done\n");
  return 0 ;
DWORD CALLBACK MyThreadProc( LPVOID lpParam ) 
  printf("In " __FUNCTION__ "\n", GetCurrentThreadId());
  // Pretend to do stuff
  printf("In " __FUNCTION__ " after Sleep()\n", GetCurrentThreadId());
  // Tell the caller thread you're done.
  // And return
  return 0 ; 
bjorn_ht 17-Feb-12 5:29am
And pls ignore the calls to GetCurrentThreadId() in the printf() statements in MyThreadProc(). They don't belong there.
abhineet.ayan.verma 17-Feb-12 5:53am
Thanks man, Thanks for your great help.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Does the code compile without errors and warnings? I Don't think so. Increasing the compiler warn level may help you finding some problems (Project settings with VC).

pData->URL[0] = 0 ;

This will initialize the first element of the array, but not the others. Possible soure for problems.

TempURL = StrDupA(pURL) ;

TempURL is not defined (compiler error). StrDup() returns a pointer to allocated memory and must be freed later using LocalFree(). Without freeing, you have memory leaks.

pData->URL[dwIndex] = (PCHAR)TempURL ;

This generates a compiler warning, because dwIndex is not initialized. Your code will probably crash.

dwTotalNumberOfURL = dwIndex ;

dwTotalNumberOfURL is not defined (compiler error). Not necessary. Can use dwIndex contains instead.

pData is allocated but not freed. Another memory leak.

DownloadFile(pData->URL, pData->DestinationPath) ;

pData->URL is an array of PCHAR pointers. You did not show the code for DownloadFile() but it may be expects a PCHAR rather PCHAR*. If it expects a PCHAR* it would be OK.

You should initialize dwIndex, fill your struct with NULL bytes, and free up memory at the end of your function:
DWORD dwIndex = 0;
// You may also omit the allocation. The array can be also on the stack.
pData = (PMAINDATA)malloc(sizeof(MAINDATA)) ;
ZeroMemory(pData, sizeof(MAINDATA));
// No longer necessary
//pData->DestinationPath = NULL ;
//pData->URL[0] = 0 ;
pData->URL[dwIndex] = (PCHAR)StrDupA(pURL) ;
for (DWORD i = 0; i < dwIndex && pData->URL[dwIndex]; i++)
return 0;

I did not know if I found all. There are too many.
CPallini 17-Feb-12 3:32am
My 5.
abhineet.ayan.verma 17-Feb-12 4:21am
I have not submitted the whole code as it is long. What you have pointed is all implemented in my Code. Can you please answer as to what is the problem with my QueueUserWorkItem Method? It is a Bool value and it is returning 1 but even then it is not calling the function "MyThreadProc" and goes to next line in Main itself.
Jochen Arndt 17-Feb-12 5:06am
OK. Please try to state the next time. It will help to focus the real problem.

How did you determine that your thread is not called? Have you added a trace command? The return value indicates that the thread has been started. So it should be active in the background downloading files.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,749
CHill60 3,380
Maciej Los 2,953
Jochen Arndt 1,935
ppolymorphe 1,820

Advertise | Privacy | Mobile
Web02 | 2.8.170524.1 | Last Updated 17 Feb 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100