Solved

how to let a multitask thread call function in main thread

Posted on 2001-09-11
12
277 Views
Last Modified: 2013-11-20
I have a CFormview sdi app activating a thread. Inter-thread communication is done through sharing of global data utilizing CCriticalsection.

   suppose my thread is mythreadfunc(), which is forever running in background.

   in mythreadfunc() function body, how do i call a function in my view class?

  eg:
     myviewfunc() is a public function in CMyView---my view class

    and i want

       mythreadfunc()
       {
          while(run)
          {
          .....
          myviewfunc()
          .....
          }
       }

can anybody post some code? or a link to a sample?
0
Comment
Question by:orange_juice
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 1

Author Comment

by:orange_juice
ID: 6475534
my criticalsection object is a global object which can be accessed by cmyview and mythreadfunc()
0
 
LVL 5

Expert Comment

by:FengYuan
ID: 6475758
Pass the pointer to the view to your thread function
0
 
LVL 2

Expert Comment

by:BogdyPtr
ID: 6475818
Declare a BOOL var in the .cpp where the thread func is defined.

BOOL run;
Use this to begin thread:
AfxBeginThread( mythreadfunc, &run );

Modify the thread func this way:
  mythreadfunc()
      {
         while(*run)
         {
         .....
         myviewfunc()
         .....
         }
      }
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 2

Expert Comment

by:BogdyPtr
ID: 6475827
Sorrry, I'v made a mistake.

Modify the thread func this way:
 mythreadfunc(LPARAM run)
     {
        while(*((BOOL*)run))
        {
        .....
        myviewfunc()
        .....
        }
     }

If it does not work declare run as BOOL *run and pass it to AfxBeginThread fnc.
0
 
LVL 1

Author Comment

by:orange_juice
ID: 6476217
BodqyPtr,
  perhaps you got me wrong,
  i already can start my thread, and let it run
   what i want is to let the thread access a function in the view class --->that myviewfunc
0
 
LVL 2

Expert Comment

by:BogdyPtr
ID: 6476321
Pass a pointer to the instance of your view class to your thread.
0
 
LVL 1

Author Comment

by:orange_juice
ID: 6476644
Feng Yuan and Boqdyptr,

    you two has some point. But i think that will make my thread unsafe---deadlock

   what if at the instance my thread access a function, my view class also access it? ---> system crash...etc

   i have been using ccriticalsection for such matter, but that is a global object, storing some shared variable.
   
    i believe there are other ways.
0
 
LVL 2

Accepted Solution

by:
bkdc earned 100 total points
ID: 6476849
orange_juice, you're right about the deadlocks and stuff. I had pretty much the same problem a while ago and i chose a different approach: messages. Anyway, there are other options too, events is just an example.
So...add a custom message handler and do the stuff using it.

wrapper.cpp

#include "your cformview custom header" //if any
......
CxFormView *instance_pointer;  //take care to initialize it before starting the thread.

... msg_handler(...)
{
  instance_pointer->myviewfunc();
}


your thread becomes smth like:
      mythreadfunc()
      {
         while(run)
         {
         .....
         PostThreadMessage(you_app_thread_id,your_message...);
//instead of myviewfunc()
//use SendMessage if you want
         .....
         }
      }

i hope it's clear enough; you should get the main ideea.
if not, I'll try later to give you a more detailed eg. (i'm in a hurry now)

0
 
LVL 1

Author Comment

by:orange_juice
ID: 6481431
Dear bkdc,
   thank you for your example....yes...pls send me a more detailed one. I think i get your idea.
   actually, i have been reading on worker thread topic in www.codeproject.com   they mention, sendmessage will cause deadlock. They use PostMessage instead Well, there are still many things i haven't read up...so, can't say anything more already.

anyway.
 
   PostThreadMessage(you_app_thread_id,your_message...);

   what will 'you_app_thread_id' be ?
0
 
LVL 2

Expert Comment

by:bkdc
ID: 6484971
>>what will 'you_app_thread_id' be ?
well, when you create a thread with whatever function you want (MFC or not) the result is the thread id.
 You may use the thread id to send messages(for eg.) to a certain thread.

 about the SendMessage thing...yes, it could cause trouble but it has it's good parts too (a few): if you wanna make sure you get your job done before returning in the thread SendMessage is the thing you want. If your threads may run in parallel without the need to sync. then PortThreadMessage is more suited.

 About the source code I promised you....I'm kind of busy now (on a *nix machine - so VC/MFC is not around). Maybe in a couple of days but I'd suggest you try do it yourself (it's more fun :)) )
0
 
LVL 1

Author Comment

by:orange_juice
ID: 6485672
yes yes, thank you
i'll try
0
 
LVL 1

Author Comment

by:orange_juice
ID: 6490177
i tried it, combination of CEvent, CCriticalSection, ....and it works

but anyway, i let my thread now reside as

CMyview::thread()
{
}

so, my thread can see my view inherently, without passing pointer....

i wonder if that will cause problem

thank you everybody
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 84
iSeries FTP Exit Program 8 137
if loop in java 3 142
Updating statistics with error notification email in SQL server 4 96
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question