?
Solved

AfxBeginThread prob

Posted on 2005-02-25
8
Medium Priority
?
419 Views
Last Modified: 2013-11-20
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
Comment
Question by:shav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13403831
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
 
LVL 48

Accepted Solution

by:
AlexFM earned 200 total points
ID: 13403998
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 13404018
Oops, sorry, it is already written in Axter's answer. Please ignore my post.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:shav
ID: 13404087
Hey Axter, my code is pretty big and don't really want to post.
Could you give me a quick example?
Thanks
           Shav
0
 

Author Comment

by:shav
ID: 13404231
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
 

Author Comment

by:shav
ID: 13404301
Thanks guys used p. instead of p->. Working now
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 13404369
Axter, sorry for hijacking your question. If you want these points, you can post request to CS.
0
 
LVL 30

Expert Comment

by:Axter
ID: 13404998
>>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

Independent Software Vendors: 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!

Question has a verified solution.

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

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 …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

800 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