Click to See Complete Forum and Search --> : not sure how to do this...


Dman832
July 22nd, 2005, 12:03 PM
I am writing a program in VC++ that is basically a big dialog box. I am trying to write a thread that will update the view of it once in a while, the main thread is usually busy with computation, so the view is never refreshed. I tried to make this thread function a member of my View class, but I need to call GetDocument() so I can't make the function static. I have never used threads in C++ before and am quite confused.

Also, if I don't make it a member function, I'm not sure I know how to get the view of the dialog so that I can call the Update function.

Can anyone help me out with this?

Arjay
July 22nd, 2005, 01:02 PM
I am trying to write a thread that will update the view of it once in a while, the main thread is usually busy with computation, so the view is never refreshed.Generally this approach isn't recommended. Usually you would have the main thread handle the UI chores and put the labor intensive code in the secondary thread. You will have some sort of shared data between the two threads which you will need to protect. You will also have to develop a mechanism to notify the main UI thread to refresh the data. The idea here is the secondary thread is busy retrieving or updating the data. When desirable, it periodically tells the UI thread to update its display.

Below are a few resources that might help you. The first is a small sample from another threading post. It uses a std::list to share data between two threads. A secondary threads adds items to the list and the main UI thread displays the items in a list control. The second two are CG articles which discuss basic threading, thread synchronization techniques, and introduce some sync helper classes.

CListView Check State Post (http://www.codeguru.com/forum/showthread.php?t=321465)

Win32 Thread Synchronization, Part I: Overview (http://www.codeguru.com/Cpp/W-D/dislog/win32/article.php/c9823/)
Win32 Thread Synchronization, Part II: Helper Classes (http://www.codeguru.com/Cpp/W-P/win32/tutorials/article.php/c9825)

Arjay

Hacker2
August 2nd, 2005, 12:41 AM
It is easy for a thread to access your class
when you create the thread, pass it a var equal to "this"
so that the thread know what instance called it.
so then in the thread, just use pThis->GetDocument()

see
http://www.flounder.com/callbacks.htm
and scroll down to
"Worker Threads in a class"