Click here to Skip to main content
15,889,542 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionEncrypted shared memory Pin
Chintoo72313-Oct-05 5:18
Chintoo72313-Oct-05 5:18 
QuestionMissing pointer between threads Pin
bispenho13-Oct-05 5:16
sussbispenho13-Oct-05 5:16 
AnswerRe: Missing pointer between threads Pin
David Crow13-Oct-05 5:42
David Crow13-Oct-05 5:42 
GeneralRe: Missing pointer between threads Pin
bispenho13-Oct-05 6:20
sussbispenho13-Oct-05 6:20 
GeneralRe: Missing pointer between threads Pin
David Crow13-Oct-05 7:12
David Crow13-Oct-05 7:12 
GeneralRe: Missing pointer between threads Pin
André RB13-Oct-05 7:31
André RB13-Oct-05 7:31 
GeneralRe: Missing pointer between threads Pin
David Crow13-Oct-05 10:18
David Crow13-Oct-05 10:18 
AnswerRe: Missing pointer between threads Pin
LighthouseJ13-Oct-05 8:19
LighthouseJ13-Oct-05 8:19 
Because you are going to be doing complex things, you should consider using a user-interface thread. Just using a single thread function (threadFunc as you described) is considered a worker thread. I have recent experience with them, and here's what you need.

1. Derive a class from CWinThread that will be used
2. Add any pointer or whatever you want to the class definition
3. Override the OnIdle function (may be automatically overridden)
  a. put all functionality in there
  b. return a nonzero value if you need more CPU time, return 0 if the thread is finished
4. In your main function where the thread will start, the thread will be started like this:
CMyThread * thread = (CMyThread *) AfxBeginThread (RUNTIME_CLASS(CMyThread), THREAD_PRIORITY_IDLE, 0, CREATE_SUSPENDED, NULL);<br />
<transfer data to the thread to be used by the thread here><br />
thread->ResumeThread();


At this point, the thread will be running.

Note: you will not create your own instance of the thread, the AfxBeginThread does all the creation of the data you need. Also, put whatever data that you would normally put in the constructor and destructor, put them in InitInstance and ExitInstance respectively. The threads constructor and destructor

Note: the thread written above will be created but will not be started automatically, hence the CREATE_SUSPENDED option. This gives you a time to pass as much data to the thread as you need. Run ResumeThread() to start the thread. At this point, the InitInstance member function will be run and can do the work of the constructor. Then the OnIdle member function will continually be called repeatedly until there is either a message to handle, or you return 0 and tell the thread no more work is needed. Message passing is considered a vital but relatively uncommon task so it's given a high priority. Since message passing is uncommon, the thread will consume practically it's entire time doing your work but it still needs to occaisionally checked and dispatched messages if needed.

You can use AfxEndThread() to stop the thread inside the threads' functions, and ExitInstance will be run to do the work of the destructor.

I've used worker threads and unless you need pure work done and aren't concerned with any message passing or anything else besides pure work, then UI threads provide many benefits.
GeneralRe: Missing pointer between threads Pin
André RB13-Oct-05 9:20
André RB13-Oct-05 9:20 
QuestionGlobals and namespace Pin
karmendra_js13-Oct-05 4:14
karmendra_js13-Oct-05 4:14 
AnswerRe: Globals and namespace Pin
Chris Losinger13-Oct-05 5:29
professionalChris Losinger13-Oct-05 5:29 
AnswerRe: Globals and namespace Pin
toxcct13-Oct-05 5:29
toxcct13-Oct-05 5:29 
AnswerRe: Globals and namespace Pin
ddmcr13-Oct-05 5:52
ddmcr13-Oct-05 5:52 
AnswerRe: Globals and namespace Pin
ThatsAlok13-Oct-05 7:35
ThatsAlok13-Oct-05 7:35 
AnswerRe: Globals and namespace Pin
Arman S.13-Oct-05 10:36
Arman S.13-Oct-05 10:36 
QuestionListCtrl Pin
karmendra_js13-Oct-05 4:07
karmendra_js13-Oct-05 4:07 
AnswerRe: ListCtrl Pin
muthuramji13-Oct-05 5:26
muthuramji13-Oct-05 5:26 
GeneralRe: ListCtrl Pin
karmendra_js14-Oct-05 0:00
karmendra_js14-Oct-05 0:00 
GeneralRe: ListCtrl Pin
muthuramji14-Oct-05 6:29
muthuramji14-Oct-05 6:29 
Questionfunction TransmitFile( ) Pin
REU13-Oct-05 2:28
REU13-Oct-05 2:28 
QuestionWho can tell me how to use Serialize Pin
Fired Fish13-Oct-05 1:55
Fired Fish13-Oct-05 1:55 
AnswerRe: Who can tell me how to use Serialize Pin
ThatsAlok13-Oct-05 2:24
ThatsAlok13-Oct-05 2:24 
AnswerRe: Who can tell me how to use Serialize Pin
ddmcr13-Oct-05 2:48
ddmcr13-Oct-05 2:48 
QuestionRe: Who can tell me how to use Serialize Pin
David Crow13-Oct-05 3:33
David Crow13-Oct-05 3:33 
AnswerRe: Who can tell me how to use Serialize Pin
Fired Fish14-Oct-05 1:39
Fired Fish14-Oct-05 1:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.