CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 30 of 30
  1. #16
    Join Date
    Apr 2008
    Posts
    93

    Re: static drawing in Worker Thread

    Quote 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()
    
    /////////////////////////////////////////////////////////////////////////////

  2. #17
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    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).
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  3. #18
    Join Date
    Apr 2008
    Posts
    93

    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?

  4. #19
    Join Date
    Apr 2008
    Posts
    93

    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.

  5. #20
    Join Date
    Feb 2002
    Posts
    3,788

    Re: static drawing in Worker Thread

    Quote 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?

  6. #21
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  7. #22
    Join Date
    Apr 2008
    Posts
    93

    Re: static drawing in Worker Thread

    Quote 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?

  8. #23
    Join Date
    Apr 2008
    Posts
    93

    Re: static drawing in Worker Thread

    Quote 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.

  9. #24
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: static drawing in Worker Thread

    Quote Originally Posted by ashwarrior
    what are the link?
    Do not see the links in my signature line?

  10. #25
    Join Date
    Apr 2008
    Posts
    93

    Re: static drawing in Worker Thread

    Quote Originally Posted by Arjay
    Do not see the links in my signature line?
    what signature?

    i didnt see any signature from your reply.

  11. #26
    Join Date
    Apr 2008
    Posts
    93

    Re: static drawing in Worker Thread

    Quote 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.

  12. #27
    Join Date
    Apr 2008
    Posts
    93

    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.

  13. #28
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: static drawing in Worker Thread

    Quote 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.

  14. #29
    Join Date
    Aug 2005
    Posts
    49

    Re: static drawing in Worker Thread


  15. #30
    Join Date
    Apr 2008
    Posts
    93

    Re: static drawing in Worker Thread

    Thanks Arjay, I can see the signatures now.


    Quote 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?

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured