C language > Windows questions

Program Freeze

(1/1)

Grincheux:

--- Code: ---unsigned long int __stdcall ImportFile(void * __lpParameter)
{
DWORD _dwFileSize ;
LPBYTE _lpBuffer ;

_dwFileSize = 0 ;

_lpBuffer = ReadTheFile(szFileToTreat,&_dwFileSize) ;
if(_lpBuffer == NULL)
return (0) ;

dwNumRecords = DecodeBuffer(hDialog,_lpBuffer,_dwFileSize) ;

VirtualFree(_lpBuffer,0,MEM_RELEASE) ;

return (1) ;
}

--- End code ---

calling the thread


--- Code: --- dwNumRecords = 0 ;
hDialog = __hWnd ;
SetDlgItemText(__hWnd,IDC_EDIT_01,"R U N N I N G") ;
_hThread = CreateThread(NULL,0,ImportFile,NULL,16 * 1024 * 1024,&_dwThreadId) ;

WaitForSingleObject(_hThread,INFINITE) ;
CloseHandle(_hThread) ;

--- End code ---



I would like to write the number of records completed into the edit field.
For now it writes but it is not shown.

How could I do?

Thanks

TimoVJL:
This blocks you code

--- Code: ---WaitForSingleObject(_hThread,INFINITE) ;
--- End code ---
Small example:
--- Code: ---#define WIN32_LEAN_AND_MEAN
#include <windows.h>

INT_PTR CALLBACK MainDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
void OnInitDialog(HWND hwndDlg);
void StartStopThr(HWND hwndDlg);

DLGTEMPLATE Dlg = {WS_POPUPWINDOW|WS_CAPTION|WS_VISIBLE|DS_3DLOOK|DS_CENTER,0,0,0,0,200,100};
DWORD DlgFill[2] = {0}; // for dialog, must be here after template in this example
HANDLE hThread1;
BOOL bRun;

int __cdecl WinMainCRTStartup(void)
{
ExitProcess(DialogBoxIndirectParam(GetModuleHandle(NULL), (LPDLGTEMPLATE)&Dlg, 0, MainDlgProc, 0));
}

INT_PTR CALLBACK MainDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
switch (LOWORD(wParam)) {
case 4001:
StartStopThr(hwndDlg);
return TRUE;
}
break;
case WM_INITDIALOG:
OnInitDialog(hwndDlg);
return TRUE;
case WM_CLOSE:
EndDialog(hwndDlg, 0);
return TRUE;
}
return FALSE;
}

void OnInitDialog(HWND hwndDlg)
{
CreateWindowEx(0,TEXT("BUTTON"),TEXT("Start"),
WS_CHILD|WS_VISIBLE,10,10,50,25,hwndDlg,(HMENU)4001,GetModuleHandle(NULL),0);
}

DWORD ThreadProc1(void* pPar)
{
int iCnt = 0;
while (bRun) {
TCHAR szTmp[100];
wsprintf(szTmp, TEXT("Thread1: %d"), iCnt++);
SetWindowText((HWND)pPar, szTmp);
Sleep(1000);
if (iCnt > 100) break;
}
SetDlgItemText((HWND)pPar, 4001, TEXT("Start"));
SetWindowText((HWND)pPar, TEXT("Thread1 Ends"));
bRun = 0;
return 0;
}

void StartStopThr(HWND hwndDlg)
{
if (bRun) {
bRun = 0; // ask to stop
//SetDlgItemText(hwndDlg, 4001, TEXT("Start"));
} else {
SetWindowText(hwndDlg, TEXT("Create Thread1"));
if ((hThread1 = CreateThread(NULL,0,ThreadProc1,hwndDlg,0,NULL)) != 0) {
SetDlgItemText(hwndDlg, 4001, TEXT("Stop"));
bRun = 1;
}
}
}
--- End code ---

Grincheux:
Thank you for the answer.

Grincheux:
I made a thread to display the count but it does not show anything. I don't understand.

Here is my project.

Could you help me... again, please.

Navigation

[0] Message Index

Go to full version