Link to home
Start Free TrialLog in
Avatar of has
has

asked on

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
Avatar of faster
faster

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.
Avatar of has

ASKER

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
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.
Avatar of has

ASKER

I will try shaig's comment, seems interesting, thank you both
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.
Avatar of has

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of rajesh032097
rajesh032097

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
Avatar of has

ASKER

yes i have carried the main dialog pointer to that anyuseful
class object to manipulate some edit boxes. thanks