Hi ..
I was able to create a workers thread using WIN API(not MFC)....but nw my problem is to create a UI thread .It is possible when we do the code in MFC. But I want to have that using Win API.
Can anybody help me out with this problem.It will be kind of you if you help me out with any code for that UI threads...
Igor Vartanov
November 3rd, 2008, 05:07 AM
And what is the problem? You create GUI in main thread and have no problem, right? So what would be the difference for another thread?
anup007star
November 3rd, 2008, 10:53 PM
The problem is in MFC we differentiate two threads as workers and UI but in Win 32 SDK programing these two are same nw I want to handle windows messages(WM_...) with the UI thread ...stuck up hw to do that...
I am using CreateThread function in which i am concerned with two arguments (the thread function --its calling and the data it is sending to that thread function).....
Let me share my code with you that i have prepared for workers thread .Please let me know the same with UI thread..In UI thread function hw can I handle WM messages.
DWORD WINAPI Thread_no_1( LPVOID lpParam )
{
int Data = 0;
int count = 0;
HANDLE hStdout =NULL;
// Get Handle To screen.
// Else how will we print?
if(hStdout == INVALID_HANDLE_VALUE )
return 1;
// Cast the parameter to the correct
// data type passed by callee i.e main() in our case.
Data = *((int*)lpParam);
void DisplayMessage (HANDLE hScreen,
char *ThreadName, int Data, int Count)
{
switch(Data)
{
case 1:
MessageBox(NULL,TEXT("Thread 1 implemented"),NULL,MB_ICONERROR);
break;
case 2:
MessageBox(NULL,TEXT("Thread 2 implemented"),NULL,MB_ICONERROR);
break;
}
Sleep(1000);
}
TheCPUWizard
November 3rd, 2008, 10:55 PM
1) Create a thread
2) Run a Message Pump on the Thread.
Viola, A UI Thread....NO magic.....
anup007star
November 4th, 2008, 04:59 AM
For UI thread, the thread should handle windows messages nw my problem is in
CreateThread(..,..,ThreadProc,&data,..)
In this i am calling the thread function to create a thread and i am passing an int data into it.....
Nw in the function ThreadProc which takes dis data as lparam ....
Nw hw can i handle a message loop inthat thread so that windows messages can get handled. Please help me with some code..Its a request
TheCPUWizard
November 4th, 2008, 07:23 AM
Just implement a classic message pump. There are hundreds of samples aleready in existance, including the source of exactly HOW MFC does it.... :rolleyes:
MikeAThon
November 4th, 2008, 10:34 AM
As an example, think for a moment of the classic C-style code for WinMain. It registers and creates a main window, shows it, and then starts a GetMessage() - TranslateMessage() - DispatchMessage() loop.
Your function for the UI thread should look exactly like this WinMain.
anup007star
November 5th, 2008, 02:58 AM
As an example, think for a moment of the classic C-style code for WinMain. It registers and creates a main window, shows it, and then starts a GetMessage() - TranslateMessage() - DispatchMessage() loop.
Your function for the UI thread should look exactly like this WinMain.
But my UI thread function has LPVOID lParam as its parameter only.
Actually i have a property sheet in which i have two pages .Nw in both the pages i have two buttons. In one pages on clicking the button i have implemented a worker thread. And in the other page when I click the other button I should be able to start a UI thread where I can handle WM messages for that page only.
Now as you are saying to create the thread function just like the winmain hw will i be able to communictae my messages back to the property sheet .
Lots of confusion........................if u can explain me with some code i will be gr8full to u...i m in dire need........below is my code that i have worked for a workers thread.Please help me with UI thread.....The commented part i have taken care in other files. Here I have Thread_no_2 function where i have to implement the UI thread...here i need guidance...
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
/*HANDLE Handle_Of_Thread_1=0;
HANDLE Handle_Of_Thread_2 = 0;
HANDLE Array_Of_Thread_Handles[2];
int Data_Of_Thread_1 = 1;
int Data_Of_Thread_2 = 2;*/
// for creating dialog box
BasePtr[0] = new DerivedClass();
BasePtr[1] = new DerivedClass1();
// BasePtr[2] = new DerivedClass2();
hModelssDialog=CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG),NULL,DlgProc);
hModelssDialog1=CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG11),hModelssDialog,DlgProc1);
if (hModelssDialog == NULL || hModelssDialog1 == NULL)
{
DWORD Result=GetLastError();
// Notified your about the failure
MessageBox(NULL, TEXT("Application failed to load!"), TEXT("Error"), MB_OK | MB_ICONEXCLAMATION);
// Set the return value
return FALSE;
}
void DisplayMessage (HANDLE hScreen,
char *ThreadName, int Data, int Count)
{
switch(Data)
{
case 1:
MessageBox(NULL,TEXT("Thread 1 implemented"),NULL,MB_ICONERROR);
break;
case 2:
MessageBox(NULL,TEXT("Thread 2 implemented"),NULL,MB_ICONERROR);
break;
}
Sleep(1000);
}
anup007star
November 5th, 2008, 05:20 AM
Also hw can I have a call back function in that thread function so to handle the WM messages.....just as we have in Win main
Igor Vartanov
November 5th, 2008, 06:49 AM
Man, just relax on asking and focus on the answers you already was given with.
Here's your main thread pseudo code
int WinMain(. . .)
{
// create window
HWND hwnd = CreateWindow(. . .);
// run message loop
MSG msg;
while (0 < GetMessage(&msg, NULL, 0, 0))
{
. . .
}
}
And here's your second GUI thread
DWORD ThreadProc(LPVOID pVoid)
{
// create window
HWND hwnd = CreateWindow(. . .);
// run message loop
MSG msg;
while (0 < GetMessage(&msg, NULL, 0, 0))
{
. . .
}
}Can you see any difference? That is what Mike was talking about.
Also hw can I have a call back function in that thread function so to handle the WM messages.....just as we have in Win mainThe answer is: just as we have it in Win main. To create window you register a window class where you provide window procedure address. Or provide dialog procedure to DialogBox in case your window is dialog.
anup007star
November 5th, 2008, 07:54 AM
thnx for your reply..... but i dnt want to create another window....actually i have a property sheet and in one of the pages i have a button clicking on it will run my UI thread which will handle things on that page only......ie., any WM messages........fact is that i m nt able to handle any WM message by this thread.
ThreadProc(LVOID lParam)--> here i dnt have hInstacnce defined just as in WinMain so if i will try to create a window by CreateWindow in that thread function ..... then one of its argument as hInstance will not be defined and will pop an error.
can you please suggest me hw can i handle the events on clicking that buton on that page using UI threading...i m in deep confusion hw to do that....need some guidance.....
Thnx in advance!!!!!!!!!!!!!!!!
TheCPUWizard
November 5th, 2008, 08:25 AM
You do NOT processes Windows Message in a thread other than the thread that created the Windows Control, nor do you update controls from a different thread.
Repeat this 100 time, until it is firmly lodged in your mind.
If you have a property sheet that was created in thread "A". The the messages (from clicks and the like) must all be processed in thread "A". If this processing involves updating the UI, it must be done from thread "A".
This leaves you with two choices.
1) Have your message processing function (which is called on thread "A") communicate with a NON-UI / Worker thread. The worker thread can post messages back to thread "A" which will perform the actual updates.
2) Create a UI thread (Examples given)...and CREATE THE UI ELEMENT on the UI thread.
MikeAThon
November 5th, 2008, 10:08 AM
TheCPUWizard is correct: You cannot handle window messages anywhere except in the thread that created the window. The Windows OS enforces this rule in a very simple way: Each thread is given its own unique message queue, and messages for each window are enqueued only in the message queue for the thread that created the window.
So, it is not possible for BN_CLICKED messages from a button (for example) to be received and processed anywhere except in the thread that created the button.
If you feel a need to do so indirectly, then you can somehow signal another thread that a button message has been received in the main thread. The manner in which you send the signal is entirely up to you. This is what TheCPUWizard was saying in his point #1.
In his point #2, do not ever create a window (i.e., a button or another UI element) where the window's parent was created in another thread. A window and all of its children must be created in the same thread.
Mike
anup007star
November 6th, 2008, 03:43 AM
Thnks to both of u ..........in clearing my doubts....
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.