Click to See Complete Forum and Search --> : Passing data from a thread to a timer function


madenai
May 30th, 2007, 08:01 AM
Hi,

I need to send data (X) from a dialog based application (which runs in a thread) to a to a timer function which plots this data.

=================================
declarations of CCLass.
.................
..................

UINT ThreadFunc (LPVOID pParam)

{

...............

X ; // X calculated here

..................

}

CClass ::OnTimer(UINT nIDEvent)
{

Plot(X); // X is ploted here

CDialog::OnTimer(nIDEvent);
}
Thanks.

Nas

MrViggy
May 30th, 2007, 01:23 PM
Pass a pointer to the instance of your CClass object to your thread function. Then, just write a member function of the class to update the value.

Viggy

Andreas Masur
May 30th, 2007, 10:14 PM
I need to send data (X) from a dialog based application (which runs in a thread) to a to a timer function which plots this data.

class CClass
{
public:
bool StartThread();

private:
SomeType X;
CWinThread* Thread;
// Some locking mechanism such as a mutex or critical section

static UINT ThreadFunc (LPVOID pParam);
};

bool CClass::StartThread()
{
Thread = AfxBeginThread(ThreadFunc, this);
return Thread ? true : false;
}

void CClass::OnTimer(UINT nIDEvent)
{
// Lock access to X
Plot(X); // X is ploted here
// Unlock access to X

CDialog::OnTimer(nIDEvent);
}

UINT CClass::ThreadFunc(LPVOID pParam)
{
CClass* parent = static_cast<CClass*>(pParam);

// Lock access to X
parent->X; // X calculated here
// Unlock access to X
}

madenai
May 31st, 2007, 03:41 AM
I have already tried this, but I have an error.


=================================
declarations of CCLass.
.................
CClass* pC;
..................

UINT ThreadFunc (LPVOID pParam)

{

...............

pC->X = ........... ; // X calculated here

..................

}

CClass ::OnTimer(UINT nIDEvent)
{

Plot(pC->X); // X is ploted here

CDialog::OnTimer(nIDEvent);
}
Thanks.

Nas

madenai
May 31st, 2007, 05:20 AM
Hi Andreas,

Thanks for you reply.
Actually my first problem is to have X available inside the timer function. I tried to do it using a global pointer "pC" to CClass. I have an error!

=============CClass.h=============
class CClass
{
public:
bool StartThread();

private:
SomeType X;
CWinThread* Thread;
// Some locking mechanism such as a mutex or critical section

static UINT ThreadFunc (LPVOID pParam); // need static here to
// avoid error C2665
};

==============CClass.cpp=====================
CClass* pC; // global pointer
.........
bool CClass::StartThread()
{
Thread = AfxBeginThread(ThreadFunc, this);
return Thread ? true : false;
}

/////////////////////////////////////////////////////
void CClass::OnTimer(UINT nIDEvent)
{

Plot(pC->X); // X is ploted here


CDialog::OnTimer(nIDEvent);
}
/////////////////////////////////////////////////////
UINT CClass::ThreadFunc(LPVOID pParam)
{
CClass* parent = static_cast<CClass*>(pParam);
pC->X = parent->X; // X calculated here

}

Thanks.

MrViggy
May 31st, 2007, 11:30 AM
Why do you refer to the global pointer inside the class? Look at Andreas' example. This is all you really need.

Viggy

Andreas Masur
May 31st, 2007, 09:21 PM
I have already tried this, but I have an error.
What error? Where is pC actually allocated? Released? Assigned? As already indicated...why do you make things more complicated than necessary? ;)

madenai
June 1st, 2007, 05:55 AM
Hi,

Yes, its working now.
Many thanks to Andreas and Viggy !!

All the best

madenai