-
May 21st, 2008, 10:27 PM
#16
Re: static drawing in Worker Thread
Originally Posted by Arjay
When any application starts up the system creates at least one thread (of execution) called the primary thread. From that point on, it is up to the individual application to create additional threads.
An MFC application is no different whether it's a dialog, SDI, or MDI based application.
The starting point of the MFC app is it's WinApp derived class, when the application starts up and this class gets created it gets created in the application's primary thread. Typically an MFC app will have some UI portion and that to will get created in the primary thread (again whether it's a dialog, SDI or MDI app). Once this UI is created, a message pump gets added to the primary thread, which essentially turns it into a UI thread.
The heirarchy for these application types in terms of the UI are:
Code:
Dialog app
CMyApp (CWinApp derived)
L CMyDialog (CDialog derived)
Code:
SDI
L CMyApp (CWinApp derived)
L CMainFrame (CFrameWnd derived)
L CMyView (CView derived)
Code:
MDI
L CMyApp (CWinApp derived)
L CMainFrame (CFrameWnd derived)
L CChildFrame (CMDIFrameWnd derived)
L CMyView (CView derived)
It is important to understand that all of these classes and windows run in the primary or UI thread of the application.
Ashwarrior, this should answer your question about not being able to find the UI thread.
As far as send a message to draw....
What you do is create a user defined message (i.e. WM_USER + 1), create a Message handler in your view (or dialog) that contains the drawing code. Keep in mind that this message handler, being in the view class, runs in the main thread.
From your non-UI, worker thread send the user defined message to the view (when you create the worker thread, you pass in an hwnd of window you wish to send messages to).
If you understand this much, then let us know and we can tell you how to pass data between threads in a thread safe manner.
ok i using dialog, so i should see the following code as the heirarchy for these application types in terms of the UI?
Code:
/////////////////////////////////////////////////////////////////////////////
// CMy123App
BEGIN_MESSAGE_MAP(CMy123App, CWinApp)
//{{AFX_MSG_MAP(CMy123App)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
-
May 22nd, 2008, 12:25 AM
#17
Re: static drawing in Worker Thread
AfxGetApp function returns pointer to CWinApp. CWinApp is derived from CWinThread. So you can always have it whenever you need it. Not required now, just FYI.
You cannot pass any MFC object (specially CWnd and derived), into thread and use them directly. Pass m_hWnd of the window (this is public member of CWnd, and accessible to dialog class also), as LPVOID paramater of CreateThread/AfxBeginThread. You must typecase it to (LPVOID/void*).
In the thread function, typecast back it to HWND (m_hWnd is of this type), and store into variable. Now here you have two options:
Use ::PostMessage API, and post message to this window directly.
Use CWnd::FromHandle, get CWnd* object and use PostMessage method.
In both approaches, you need to handle it via, ON_MESSAGE in your message map; and Post the same message (some WM_USER + n value).
-
May 22nd, 2008, 11:32 PM
#18
Re: static drawing in Worker Thread
ok now my lecture has another idea which i think will not work but still have to ask.
i will be reading hexadecimal value (ie. 0x56E5) from a database and i put the database function in a thread. when there is input detected from the prototype product, it will trigger the static drawing function.
but from what i heard previously from the lecturer that when you use thread you dont need to use ONTIMER anymore for its to run but the static drawing function do not belong in thread at all, its a solo function.
but to run a static drawing function i need a ONTIMER to run. or is there any other way to run it without ONTIMER?
-
May 27th, 2008, 04:26 AM
#19
Re: static drawing in Worker Thread
i manage to get the static drawing to appear when i click OK button on the dialog box. but for the actual program, i want it to appear when certain conditions are satisfy. not by button
the attachment is the static drawing inside the thread and appear using button.
-
May 27th, 2008, 01:04 PM
#20
Re: static drawing in Worker Thread
Originally Posted by ashwarrior
....but for the actual program, i want it to appear when certain conditions are satisfy...
like, what conditions exactly? do we have to guess?
-
May 27th, 2008, 01:51 PM
#21
Re: static drawing in Worker Thread
Before you add anymore functionality, I suggest you modify your program so it starts and stops the threads cleanly.
Currently, your program just creates a couple of threads that run wild - even the debugger has trouble stopping your program.
If you haven't already, I suggest you read the articles listed in my subject line - they show you how to properly start, pause, resume, stop and cleanup threads. These are starting points of multithreading. Once you master this, then you can add additional functionality.
-
May 27th, 2008, 08:01 PM
#22
Re: static drawing in Worker Thread
Originally Posted by Arjay
Before you add anymore functionality, I suggest you modify your program so it starts and stops the threads cleanly.
Currently, your program just creates a couple of threads that run wild - even the debugger has trouble stopping your program.
If you haven't already, I suggest you read the articles listed in my subject line - they show you how to properly start, pause, resume, stop and cleanup threads. These are starting points of multithreading. Once you master this, then you can add additional functionality.
what are the link?
-
May 27th, 2008, 08:08 PM
#23
Re: static drawing in Worker Thread
Originally Posted by Alin
like, what conditions exactly? do we have to guess?
if variable BATT is been pressed (logic 1) on the prototype product, the static drawing will appear.
-
May 27th, 2008, 11:15 PM
#24
Re: static drawing in Worker Thread
Originally Posted by ashwarrior
what are the link?
Do not see the links in my signature line?
-
May 28th, 2008, 02:15 AM
#25
Re: static drawing in Worker Thread
Originally Posted by Arjay
Do not see the links in my signature line?
what signature?
i didnt see any signature from your reply.
-
May 28th, 2008, 02:46 AM
#26
Re: static drawing in Worker Thread
Originally Posted by Arjay
Before you add anymore functionality, I suggest you modify your program so it starts and stops the threads cleanly.
Currently, your program just creates a couple of threads that run wild - even the debugger has trouble stopping your program.
If you haven't already, I suggest you read the articles listed in my subject line - they show you how to properly start, pause, resume, stop and cleanup threads. These are starting points of multithreading. Once you master this, then you can add additional functionality.
sorry, do you mean i need to stop the Thread?
the program im doing now, drawing will appears when certain button is detected (pressed) and it will stay it all the way until that certain button is reset to it previous position then I will use a memory dc (Maximise black dialog box) to appear over all the drawing. To stop the program just need to press Enter button to exit back.
-
May 28th, 2008, 07:10 AM
#27
Re: static drawing in Worker Thread
this is the new code i have amend and this time i can run without OnButton1 and 2. when i run it, sometime can show out the drawings but sometime only one drawing show out or none of the drawing can show out.
like the drawing cant stay put, must play hide and seek. must run many times then can see both drawing.
-
May 28th, 2008, 08:18 AM
#28
Re: static drawing in Worker Thread
Originally Posted by ashwarrior
what signature?
i didnt see any signature from your reply.
You need to turn the signatures on. From your User CP, choose Edit options, scroll about halfway down to 'thread display options', and check 'display signatures'.
Please read these articles.
-
May 28th, 2008, 09:50 AM
#29
Re: static drawing in Worker Thread
-
May 28th, 2008, 08:03 PM
#30
Re: static drawing in Worker Thread
Thanks Arjay, I can see the signatures now.
Originally Posted by ashwarrior
this is the new code i have amend and this time i can run without OnButton1 and 2. when i run it, sometime can show out the drawings but sometime only one drawing show out or none of the drawing can show out.
like the drawing cant stay put, must play hide and seek. must run many times then can see both drawing.
can someone tell me what wrong with this code i have amended?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|