本文介绍了QueueUserWorkItem不调用该函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
为什么我的QueueUserWorkItem根本无法正常工作.我的代码在这里:

Hi All,
Why my QueueUserWorkItem is not at all working. My code is here:

typedef struct
{
    PCHAR URL[MAX_URL] ;
    PCHAR DestinationPath ;
}MAINDATA, *PMAINDATA ;

int main(int argc, char * argv[])
{
	PMAINDATA	pData ;

	BOOL bQuwi ;
	DWORD dwIndex ;
	PCHAR pURL ;

        pData = (PMAINDATA)malloc(sizeof(MAINDATA)) ;
	pData->DestinationPath = NULL ;
	pData->URL[0] = 0 ;

        do
	{

		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 ;
} 



我还没有提交完整的代码.因此,请绕过声明和可用内存错误.我面临的唯一问题是,尽管QUWI返回TRUE,但QUWI并未调用我的"MyThreadProc"函数.



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.

推荐答案

#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");
  else
    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
  CloseHandle(g_syncEventHandle);

  printf("main() done\n");
  return 0 ;
}

DWORD CALLBACK MyThreadProc( LPVOID lpParam )
{
  printf("In " __FUNCTION__ "\n", GetCurrentThreadId());

  // Pretend to do stuff
  Sleep(5000);

  printf("In " __FUNCTION__ " after Sleep()\n", GetCurrentThreadId());

  // Tell the caller thread you're done.
  SetEvent(g_syncEventHandle);

  // And return
  return 0 ;
}


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(编译器错误). StrDup()返回指向已分配内存的指针,以后必须使用LocalFree()释放它.如果不释放空间,则会发生内存泄漏.


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 ;


由于未初始化dwIndex,因此会生成编译器警告.您的代码可能会崩溃.


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

dwTotalNumberOfURL = dwIndex ;


未定义dwTotalNumberOfURL(编译器错误).不必要.可以改用dwIndex包含.

pData已分配但未释放.另一个内存泄漏.


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->URLPCHAR指针的数组.您没有显示DownloadFile()的代码,但是可能期望使用PCHAR而不是PCHAR*.如果期望PCHAR*就可以了.

您应该初始化dwIndex,用NULL字节填充结构,并在函数末尾释放内存:


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++)
    LocalFree(pData->URL[dwIndex]);
free(pData);
return 0;



我不知道是否能找到全部.有太多.



I did not know if I found all. There are too many.


这篇关于QueueUserWorkItem不调用该函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:52