Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

AfxBeginThread prob

Hi all,
I'm a newbie to this whole C++ lark and at the same time I've decided to jump 2 feet first into MFC as well. It's the sink or swim attitude. Anyway I've written a dialog based MFC program and I've come up stumps on starting a new thread. From looking at other posts, I can see people have had the same issue as me, turning a member process into a new thread.
From the other posts I can see that you are only allowed to create a static function or use a global function. That's ok, but if I do either of these, how can I access the rest of the variables in my class? I use an read only edit box, as a form of output for my program. Kinda lost on what steps to take next. Could someone explain in stupid person step, what I need to do. I've tried using the 2 solutions but came into problems, I couldn't access member varibales from static functions, I create a new object of my class but couldn't write to my output, ....
BTW, I found this post which says that it's possible to do it without using static or global functions. However, it's a bit over my head. http://www.codeproject.com/threads/memberthread.asp
Thanks all
Shav
0
shav
Asked:
shav
  • 3
  • 3
  • 2
1 Solution
 
AxterCommented:
Hi shav,
Please post your current code.

What you want to do is call the thread with a pointer to a current instance of your class.
Then your static function takes the generic pointer, and cast it back to a pointer of your class type.

It's easier to show you with your actual code.


Cheers!
0
 
AlexFMCommented:
AfxBeginThread has LPVOID pParam parameter which is used to pass user information. Pass "this" pointer in pParam. In the thread function cast it to the class pointer.

AfxBeginThread(ThreadProc, this);

UINT ThreadProc( LPVOID pParam )
{
    CMyClass* p = (CMyClass*) pParam;
    // work with class instance using p
}
0
 
AlexFMCommented:
Oops, sorry, it is already written in Axter's answer. Please ignore my post.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
shavAuthor Commented:
Hey Axter, my code is pretty big and don't really want to post.
Could you give me a quick example?
Thanks
           Shav
0
 
shavAuthor Commented:
This is the test_loop I'm using


UINT CTransmitterDlg::test_loop(LPVOID pParam)
{
      CTransmitterDlg* p = (CTransmitterDlg*) pParam;
      p.output+="in here\r\n";
      return 1;
}

I get the error C2228: left of '.output' must have class/struct/union type when trying to compile
0
 
shavAuthor Commented:
Thanks guys used p. instead of p->. Working now
0
 
AlexFMCommented:
Axter, sorry for hijacking your question. If you want these points, you can post request to CS.
0
 
AxterCommented:
>>Axter, sorry for hijacking your question. If you want these points, you can post request to CS.

Don't worry about it.  You earn them. :-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now