Re: Methods and Threading

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



FefeOxy wrote:
Dear Tom,

I am not sure if I understand what your new class does (what is
DECLARE_DYNAMIC. I read it but was confused).

My backgroundCalc function needs to access several private members such
as GPIBDevice1, 2, and the a static text member in the form (all of
which I've declared within the form). Therefore, using a static member
outside of the function is extremely undesirable. And my project being
of a managed type, I can't use friend to resolve this problem so
basically I'll need to have a bunch of delegates getting info from the
GPIBDevices and updating them in the form, that seems extremely
inefficient and long-winded.

Maybe it's time I re-structure the code.....

You actually have two problems. The first is that the thread function MUST be a static function. That problem is easy to solve: Pass your class 'this' pointer as the thread parameter. The static function can then use the pointer to call a nonstatic function. So with one extra step you can run your thread code in a member function.

2nd: Your thread will not be able to access your form or its child controls. With proper synchronization you can safely access your own variables, but not CWnd functions.

Yes, this can be awkward. It is part of the inherent limitations of using multithreading.

--
Scott McPhillips [VC++ MVP]

.



Relevant Pages

  • Re: about static variable
    ... Error 3 fatal error LNK1120: ... it cannot access member variables of your class. ... One solution is to make _userName static as well (it will then be shared by all class instances), the other is not to make testa static function. ...
    (microsoft.public.platformsdk.security)
  • Re: static functions
    ... > associated with an instance of a class like a member function is. ... I have seen people seeing a static function using ... Regards. ...
    (comp.lang.cpp)
  • Re: Casting of pointer to member functions
    ... cast it back before any usage. ... The example above will compile only if /ymg compiler switch ... the bits that pointer is ... calling a member function through such pointer is ...
    (microsoft.public.vc.language)
  • Re: Problem bei for Anweisung
    ... wenn man ein char-Feld hat. ... — if it has pointer type, it is initialized to a null pointer; ... if it is an aggregate, every member is initialized ... according to these rules; ...
    (de.comp.lang.c)
  • Re: Class members, pointers or not?
    ... > Is it best to declare class member objects as pointers or not pointers? ... using pointer members (and reference members) ...
    (comp.lang.cpp)