Click to See Complete Forum and Search --> : Cannot call a class member function from thread


Biancazzurri
October 2nd, 2005, 05:24 PM
Hi, I have a problem:

3 classes:
Opengl.h

1)
class OpenGL
{
private:

static HGLRC hRC;// Rendering Context
static HDC hDC; //Device Context
static HWND hWnd;
static HINSTANCE hInstance;
static bool active;
static bool fullscreen;

public:

OpenGL ();
~OpenGL ();
static bool Active ();
static bool Fullscreen ();
static void SetActive (bool a);
static void SetFullscreen (bool f);
static LRESULT CALLBACK WndProc (HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
static GLvoid ReSizeGLScene (GLsizei width, GLsizei height);
static int InitGL (GLvoid);
static GLvoid KillGLWindow (GLvoid);
static BOOL CreateGLWindow (char* title, int width, int height, int bits, bool fullscreenflag);
static int DrawGLBasicScene (GLvoid);
static int DrawGLScene (GLvoid);
};

Opengl.cpp

...
...
...
OpenGL graphics; // this creates opengl window

this class opens a new opengl window, main functions are:
DrawGLBasicScene - draw a black screen
DrawGLScene - draw a random pixel
these functions work on single threaded program


2)
class Thread
{
protected:
HANDLE thread_handler;
bool running;
bool suspended;
public:
Thread ();
~Thread ();
static UINT runProcess (LPVOID p);
virtual void execute ();
void endThread ();
void pauseThread ();
void resumeThread ();
bool isRunning ()
{
return running;
}
bool isPaused ()
{
return suspended;
}

class workAround{
public:
Thread* this_thread;
};
};

Thread::Thread()
{

workAround *wa = new workAround;
running = true;
suspended = false;

wa->this_thread = this;
//create a thread
thread_handler = ::CreateThread(NULL,0,
(unsigned long (__stdcall *)(void *))this->runProcess,
(void *)wa,
0,NULL);
}

Thread::~Thread()
{
}

UINT Thread::runProcess(LPVOID p)
{
workAround *wa = (workAround*)p;
Thread *t = wa->this_thread;
t->execute();
return TRUE;
}

void Thread::endThread()
{
running = false;
suspended = true;
}

void Thread:: pauseThread()
{
suspended = true;
}

void Thread::resumeThread()
{
suspended = false;
}

void Thread::execute()
{
}


this thread is the father class to all the threads

the main thread code

3)
class Thread1: public Thread
{
private:
OpenGL* a;
int i;

public:
Thread1();
Thread1(OpenGL h)
{
i = 0;
a = &h;
}
~Thread1();
void execute();
};


Thread1::Thread1()
{
}

Thread1::~Thread1()
{
}

void Thread1::execute()
{
while(running)
{
i++;
OpenGL:: DrawGLScene();
Sleep(5);
}
}



the thread each time draws a random pixel USING the current opengl window(its static for the whole program)

Main

int WINAPI WinMain( HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
Thread1 t;

MSG msg;
BOOL done=FALSE;

while(!done)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
if (msg.message==WM_QUIT)
done=TRUE;
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
if (OpenGL::Active())
{
OpenGL:: DrawGLBasicScene();
}
}
OpenGL::KillGLWindow();

t.endThread();
return 0;
}





Now the problem is that the thread1 instance doesn't do anything!(it doesn't draw!)
but when i wrote the execute function code(the one that runs while the thread is 'online')
to write some stuff into the file - it did work.
Please help. thanks.

Arjay
October 3rd, 2005, 12:27 PM
Is there any particular reason for making all the members 'static'?

Arjay

Biancazzurri
October 3rd, 2005, 12:50 PM
wndproc has to be static, otherwise i get error.
thus all the class properties are static, thus its logical to make all the functions, which access and change data, static.

I'm still stuck...

MikeAThon
October 3rd, 2005, 05:29 PM
Where are you creating a window to draw on?