2 Process at a time in Win 32 App
-
Hello All, I have a timer in my dialog which starts in the function and some process is done and I have called the stop timer as below. working code:-
int OnStart(HWND hwnd) { // Start the timer dwLastTickCount = GetTickCount(); SetTimer(hwnd, ID_ELASPED_TIMER, 25, NULL); pA->start(); KillTimer(hwnd, ID_ELASPED_TIMER); return 0; }
Now the problem is timer clock is not displayed on the dialog, once the process is over the timer is displayed. Can one please tell me how to do 2 process at a time ??? displaying the timer clock on the dialog and doing the process ?? I tried with thread as below its not working what is the error ?? what changes I have to make to thread ??int OnStart(HWND hwnd) { HANDLE hr; hr = CreateThread(NULL,0,(unsigned long (__stdcall *)(void *))WorkerThreadProc,0,0,0); return 0; } // Declaration UINT WorkerThreadProc(LPVOID Param); UINT WorkerThreadProc(LPVOID Param) { HWND hwnd; IMyA *pA; // Start the timer dwLastTickCount = GetTickCount(); SetTimer(hwnd, ID_ELASPED_TIMER, 25, NULL); pA->start(); KillTimer(hwnd, ID_ELASPED_TIMER); return true; }
Thanking you, Suresh HC. -
Hello All, I have a timer in my dialog which starts in the function and some process is done and I have called the stop timer as below. working code:-
int OnStart(HWND hwnd) { // Start the timer dwLastTickCount = GetTickCount(); SetTimer(hwnd, ID_ELASPED_TIMER, 25, NULL); pA->start(); KillTimer(hwnd, ID_ELASPED_TIMER); return 0; }
Now the problem is timer clock is not displayed on the dialog, once the process is over the timer is displayed. Can one please tell me how to do 2 process at a time ??? displaying the timer clock on the dialog and doing the process ?? I tried with thread as below its not working what is the error ?? what changes I have to make to thread ??int OnStart(HWND hwnd) { HANDLE hr; hr = CreateThread(NULL,0,(unsigned long (__stdcall *)(void *))WorkerThreadProc,0,0,0); return 0; } // Declaration UINT WorkerThreadProc(LPVOID Param); UINT WorkerThreadProc(LPVOID Param) { HWND hwnd; IMyA *pA; // Start the timer dwLastTickCount = GetTickCount(); SetTimer(hwnd, ID_ELASPED_TIMER, 25, NULL); pA->start(); KillTimer(hwnd, ID_ELASPED_TIMER); return true; }
Thanking you, Suresh HC.Suresh H wrote:
SetTimer(hwnd, ID_ELASPED_TIMER, 25, NULL);
how did u get the handle hwnd in the thread function? As per your code its not valid handle. You need to pass this handle to the
WorkerThreadProc
from theOnStart
function. Modify it as belowint OnStart(HWND hwnd) { HANDLE hr; hr = CreateThread(NULL,0,(unsigned long (__stdcall *)(void *))WorkerThreadProc,(LPVOID)hwnd,0,0); return 0; }
UINT WorkerThreadProc(LPVOID Param) { HWND hwnd = (HWND)Param; IMyA *pA; // Start the timer dwLastTickCount = GetTickCount(); SetTimer(hwnd, ID_ELASPED_TIMER, 25, NULL); pA->start(); KillTimer(hwnd, ID_ELASPED_TIMER); return true; }
nave
-
Suresh H wrote:
SetTimer(hwnd, ID_ELASPED_TIMER, 25, NULL);
how did u get the handle hwnd in the thread function? As per your code its not valid handle. You need to pass this handle to the
WorkerThreadProc
from theOnStart
function. Modify it as belowint OnStart(HWND hwnd) { HANDLE hr; hr = CreateThread(NULL,0,(unsigned long (__stdcall *)(void *))WorkerThreadProc,(LPVOID)hwnd,0,0); return 0; }
UINT WorkerThreadProc(LPVOID Param) { HWND hwnd = (HWND)Param; IMyA *pA; // Start the timer dwLastTickCount = GetTickCount(); SetTimer(hwnd, ID_ELASPED_TIMER, 25, NULL); pA->start(); KillTimer(hwnd, ID_ELASPED_TIMER); return true; }
nave
-
Suresh H wrote:
SetTimer(hwnd, ID_ELASPED_TIMER, 25, NULL);
how did u get the handle hwnd in the thread function? As per your code its not valid handle. You need to pass this handle to the
WorkerThreadProc
from theOnStart
function. Modify it as belowint OnStart(HWND hwnd) { HANDLE hr; hr = CreateThread(NULL,0,(unsigned long (__stdcall *)(void *))WorkerThreadProc,(LPVOID)hwnd,0,0); return 0; }
UINT WorkerThreadProc(LPVOID Param) { HWND hwnd = (HWND)Param; IMyA *pA; // Start the timer dwLastTickCount = GetTickCount(); SetTimer(hwnd, ID_ELASPED_TIMER, 25, NULL); pA->start(); KillTimer(hwnd, ID_ELASPED_TIMER); return true; }
nave
hi Naveen, I am getting some problem with the handle HWND & hwnd. in thread i am trying to extract the Edit box contents but i am getting the junk values, why i am getting the junk values ?? what is wrong in the code ??? same code was working in the OnStart function, when I moved it to thread is not working what is cause ???
UINT WorkerThreadProc(LPVOID Param) { HWND hwnd = (HWND)Param; char *FName = new char[20]; HWND HEFName = GetDlgItem(hwnd, IDC_FILE_NAME); SendMessage(HEFName , EM_GETLINE, 0 ,(LPARAM)FName); }
output :- ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ -
hi Naveen, I am getting some problem with the handle HWND & hwnd. in thread i am trying to extract the Edit box contents but i am getting the junk values, why i am getting the junk values ?? what is wrong in the code ??? same code was working in the OnStart function, when I moved it to thread is not working what is cause ???
UINT WorkerThreadProc(LPVOID Param) { HWND hwnd = (HWND)Param; char *FName = new char[20]; HWND HEFName = GetDlgItem(hwnd, IDC_FILE_NAME); SendMessage(HEFName , EM_GETLINE, 0 ,(LPARAM)FName); }
output :- ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍSuresh H wrote:
SendMessage(HEFName , EM_GETLINE, 0 ,(LPARAM)FName);
MSDN says
lParam: Pointer to the buffer that receives a copy of the line. Before sending the message, set the first word of this buffer to the size, in TCHARs, of the buffer. For ANSI text, this is the number of bytes; for Unicode text, this is the number of characters. The size in the first word is overwritten by the copied line.
so u should set the first 2 byte of buffer as size of the array.char *FName = new char[20]; WORD* pWord = (WORD*)FName; *pWord = 20; HWND HEFName = GetDlgItem(hwnd, IDC_FILE_NAME); SendMessage(HEFName , EM_GETLINE, 0 ,(LPARAM)FName);
] if the edit control is a single line edit I suggest you to use GetWindowText() function.nave
-
Suresh H wrote:
SendMessage(HEFName , EM_GETLINE, 0 ,(LPARAM)FName);
MSDN says
lParam: Pointer to the buffer that receives a copy of the line. Before sending the message, set the first word of this buffer to the size, in TCHARs, of the buffer. For ANSI text, this is the number of bytes; for Unicode text, this is the number of characters. The size in the first word is overwritten by the copied line.
so u should set the first 2 byte of buffer as size of the array.char *FName = new char[20]; WORD* pWord = (WORD*)FName; *pWord = 20; HWND HEFName = GetDlgItem(hwnd, IDC_FILE_NAME); SendMessage(HEFName , EM_GETLINE, 0 ,(LPARAM)FName);
] if the edit control is a single line edit I suggest you to use GetWindowText() function.nave
-
Hi Naveen, Once again thank you very much for the response. Now i am getting the values properly. Thank you very much for explanation its very use full. I had used the same code in the OnStart() and it was working properly ,but how ??
Suresh H wrote:
and it was working properly ,but how ??
may be because the following reason. When you new some data, the memory allocated contains some junk values. when you call the function from OnStart(), the first two byte might had some valid positive values. Then there is a chance for that function to work properly. Just a guess :)
nave
-
Suresh H wrote:
and it was working properly ,but how ??
may be because the following reason. When you new some data, the memory allocated contains some junk values. when you call the function from OnStart(), the first two byte might had some valid positive values. Then there is a chance for that function to work properly. Just a guess :)
nave
-
In addition to what Naveen said, read this article on creating and dealing with worker threads ->Using Worker Threads[^] There is a sub topic "Worker threads and the GUI II: Don't touch the GUI" that specifically spells out to not to let other threads touch the UI and why its bad. Best of luck.
I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley: