NO

Author Topic: Program Freeze  (Read 307 times)

Offline Grincheux

  • Member
  • *
  • Posts: 178
  • Never be pleased, Always Improved
    • Today, it's yestirday and Tomorrow
Program Freeze
« on: January 08, 2017, 10:40:41 AM »
Code: [Select]
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) ;
}

calling the thread

Code: [Select]
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) ;



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
« Last Edit: January 08, 2017, 10:44:25 AM by Grincheux »
-------------------
Today, it's yestirday and Tomorrow
Kenavo

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1561
Re: Program Freeze
« Reply #1 on: January 09, 2017, 10:35:12 AM »
This blocks you code
Code: [Select]
WaitForSingleObject(_hThread,INFINITE) ;Small example:
Code: [Select]
#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;
}
}
}
« Last Edit: January 09, 2017, 11:40:18 AM by TimoVJL »
May the source be with you

Offline Grincheux

  • Member
  • *
  • Posts: 178
  • Never be pleased, Always Improved
    • Today, it's yestirday and Tomorrow
Re: Program Freeze
« Reply #2 on: January 09, 2017, 02:52:46 PM »
Thank you for the answer.
-------------------
Today, it's yestirday and Tomorrow
Kenavo

Offline Grincheux

  • Member
  • *
  • Posts: 178
  • Never be pleased, Always Improved
    • Today, it's yestirday and Tomorrow
Re: Program Freeze
« Reply #3 on: January 17, 2017, 11:53:31 AM »
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.
-------------------
Today, it's yestirday and Tomorrow
Kenavo