Using AfxBeginThread

I have A dialog Box Application;

with push of calculate button my calculation should start but
dialog box should be responsive during calculation takes place.

my way is not a good way but is:

header file  contains->

private:
  static UINT calc(LPVOID pParam);
 void calculate();
 anyusefulclass any; // may be any class used in calculation

so on,

cpp file contains ->

void MyDlg::OnButtonCalc()
{
   AfxBeginThread(Calc,this);   // i know, should not use pointer
}

UINT MyDlg::Calc(LPVOID pParam)
{
  MyDlg* this_dlg;
  this_dlg = (MyDlg*) pParam;
  this_dlg->calculate();
  return 0;
}

void  MyDlg::calculate()
{
  a = 5 + 8; // simple calculation
 any.doit(); // calculation involving declared any object
}



now this workes under release version. but debug version
gives assertion error, on line 856 of wincore.cpp saying i
should not pass c++ objects. if i do not use any.doit() in
my calculation function, both debug and release works fine.

however, i need a better way. I tried to pass m_hWnd (as suggested in wincore.cpp) and ->

void MyDlg::OnButtonCalc()
{
   AfxBeginThread(Calc,m_hWnd);   // i know, should not use pointer
}

UINT MyDlg::Calc(LPVOID pParam)
{
  MyDlg* this_dlg;
  HWND hWnd;
  hWnd =pParam;
 //this_dlg->Attach(hWnd);
  this_dlg = (MyDlg*) Cwnd::FromHandle(hWnd);
  this_dlg = (MyDlg*) pParam;
  this_dlg->calculate();
  return 0;
}

itried this with and without attach, did not work, what is the best way to activate a member function on a separate thread, thanks
hasAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fasterCommented:
The 2nd method will never work, since MyDlg is your class and hWnd is only one of its member variable, you not expect to restore a class simply from such a window handle.

However, your first approach seems to be fine, except that CalC should not be declared as a member function (and you need not it to be one).  You must remember that a C++ function takes one more parameter (this) than it seems to need in prototype.  However AfxBeginThread() will not take care of this.

So simply try to declare CalC as a separate function (does not belong to any class) and see how.

Hope this helps.
0
hasAuthor Commented:
yes I know that, if it is a separate function it will work for sure,
if it is global, it will work, but my point is to assign a member function
to a separate thread ! so please i need answers on this direction
0
shaigCommented:
I can't answer since the question is locked... try this:

create through the class wizard a new class that inherits from CWinThread.
inplement it's InitInstanc to do your calc. ( the calc. func. can be a member variable of the threads class, passed in it's constroctor  ).

return from the initInstance FALSE that will terminate the thread.
you can wait on the handler of the thread for knowing when it's terminated ( WaitForSingleObject() ).
A code with the same idea is the one of the application's in a dialog based project.
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

hasAuthor Commented:
I will try shaig's comment, seems interesting, thank you both
0
fasterCommented:
I don't understand why you want this function to be a member of a class.  Even if it is, the "this" pointer has to be passed in a different way and therefore lose all the benifit of being a member function.  You see, in your CalC function, you will simply call Calculate() which is a TRUE member function, all the work is done there.  Actually you can define your CalC() function as being static, since it does not access anything of the class, right?  So it really makes no difference whether it belongs to the class or not.
0
hasAuthor Commented:
i have so many objects used im my calculation ( see my declaration)

anyusefulclass any; // may be any class used in calculation

and

any.doit(); // calculation involving declared any object

see my question. if I do not have this "any" object or if I create it dynamically, then no problem. as it is no way. namely "any" object
ruins it with my first method.
0
rajesh032097Commented:
Hi,
From your description, it seems that everything is ok. There is no harm in using static member functions for worker threads. The program should run without any problem like assertions. The only thing that is not clear is what your "anyusefulclass" is doing. What type of class it is and what exactly it's doing in it's "doit()" function. From the assertion that you are getting, one thing is clear that somewhere in your worker thread you are tampering with any of the windows, either the dialog box, it's own child controls or some other window. Since you are getting the assertion only when doit() is called, apparently you are doing the prohibited thing in this function itself. So please verify it again. So make sure there is no window associated with the anyusefulclass. The reason is like this:
Whenever you create a window. MFC stores it's pointer in two maps, one is a permanent map in which pointers to windows created expicitly by user are stored and the other map is a temporary one in which pointers to windows such as dialog controls are stored. These maps are specific to a thread, so your main application will have a set of these maps and if you create another thread then that thread will have it's own map set. The assertion you are getting is exactly due to this problem. Hope this solves your problem.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hasAuthor Commented:
yes i have carried the main dialog pointer to that anyuseful
class object to manipulate some edit boxes. thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.