Link to home
Start Free TrialLog in
Avatar of orange_juice
orange_juice

asked on

how to let a multitask thread call function in main thread

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?
Avatar of orange_juice
orange_juice

ASKER

my criticalsection object is a global object which can be accessed by cmyview and mythreadfunc()
Pass the pointer to the view to your thread function
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()
         .....
         }
      }
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.
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
Pass a pointer to the instance of your view class to your thread.
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.
ASKER CERTIFIED SOLUTION
Avatar of bkdc
bkdc
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ?
>>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 :)) )
yes yes, thank you
i'll try
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