|
Chuck O'Toole wrote:
Oh well, must be getting old.
Happens...
Yeah, as a rule of thumb though, I like to tell people to PostMessage() when doing things between threads (since they're supposed to be independent anyway). As you probably know, the synchronicity of SendMessage() has the potential for deadlocks so it should only be used when you truly need it.
|
|
|
|
|
Ok, instead of modifying my original question I will continue the thread since it has been really helpful. So, my program uses the following code to create a new thread.
m_Thread = AfxBeginThread(Acq_Data,this,THREAD_PRIORITY_HIGHEST);
This thread is told to look at the current class and the controlling function is Acq_Data . In Acq_Data there is the following code.
while(dlg->m_Continue && ! dlg->m_inst->m_Flags.Halt)
{
dlg->m_inst->ADCdbInquire(dlg->m_halfdata,hdlg,MSG_DRAW_SPECTRUM);
} The ADCdbInquire() function contains a call to PostMessage()
bool CPXI::ADCdbInquire(short *halfbuffer, HWND hwnd ,int Message)
{
short iStatus, iHalfReady, iDAQstopped = 0;
unsigned long ulPtsTfr=1500;
iStatus = DAQ_DB_HalfReady(m_6052_Device, &iHalfReady,
&iDAQstopped);
if ((iHalfReady == 1) && (iDAQstopped == 0))
{
iStatus = DAQ_DB_Transfer(m_6052_Device, halfbuffer,
&ulPtsTfr, &iDAQstopped);
NIDAQErrorHandler(iStatus, "DAQ_DB_Transfer",m_SuppressErrors);
if(Message != -1)
PostMessage(hwnd,WM_COMMAND,Message,NULL);
return true;
}
return false;
}
and is sending a custom message defined in the header file.
#define MSG_DRAW_SPECTRUM (WM_APP + 5) This custom messages refers to member function OnDrawSpectrum() where the following code exists to update the window.
void CDlg_SpectrumAnalyzer::OnDrawSpectrum()
{
int starti,finishi;
starti = m_datacount * m_BuffSize / 2;
finishi = (m_datacount + 1)* m_BuffSize / 2;
if(finishi > m_AcqNPts)
finishi = m_AcqNPts;
int j = 0;
for(int i = starti; i < finishi; i++)
{
m_fftdata[i].re = m_halfdata[j++];
m_fftdata[i].im = 0.;
}
m_datacount++;
if((m_datacount == m_NBuffs))
{
OnSpectrumStart();
float freq = 0;
float stepsize = ((float)(m_MaxFreq)) / ((float)(m_AcqNPts-1)/2.0);
m_datacount = 0;
m_Graph->ClearGraph(m_graphNum,false);
fftw_one(m_the_plan, m_fftdata, NULL);
for(int i = m_AcqNPts-1; i >= m_AcqNPts/2; i-= 1)
{
m_fftdata[i].re = sqrt(m_fftdata[i].re * m_fftdata[i].re +
m_fftdata[i].im * m_fftdata[i].im);
if(m_LogY)
m_fftdata[i].re = log(m_fftdata[i].re);
if(m_RtHz)
m_Graph->AddPoint(m_graphNum,sqrt(freq),m_fftdata[i].re,false);
else
m_Graph->AddPoint(m_graphNum,freq,m_fftdata[i].re,false);
freq += stepsize;
}
if(!m_LogY)
m_Graph->SetAxisProps(_T("Intensity"), _T("I"), 2, GRAPH_Y_AXIS,false);
else
m_Graph->SetAxisProps(_T("log Intensity"), _T("log(I)"), 2, GRAPH_Y_AXIS,false);
if(m_RtHz)
m_Graph->SetAxisProps(_T("Rt Frequency"), _T("Rt Hz"), 2, GRAPH_X_AXIS, false);
else
m_Graph->SetAxisProps(_T("Frequency"), _T("Hz"), 2, GRAPH_X_AXIS, false);
EnableItems(true);
m_Graph->UpdateWindows(GRAPH_WUV_ALL);
}
}
So, my question...in order to get this sequence to run several times, say 5 times, and update the window how would I use PostMessage() ? Or if I have misunderstood something please let me know.
Thanks again for all the help.
|
|
|
|
|
AndrewG1231 wrote: So, my question...in order to get this sequence to run several times, say 5 times, and update the window how would I use PostMessage() ? Or if I have misunderstood something please let me know. See here for more.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
Thanks for the link to the article and I have tried to implement the PostMessage() in my code.
However, I am still having problems trying to get it to execute more than once and update the window (looping the code is not working). After reading your suggested article, I am still not sure how to do this correctly, can you help?
My code is as follows.
void CDlg_SpectrumAnalyzer::OnBtnClickedRun()
{
PostMessage(WM_COMMAND,IDC_SPECTRUM_START,0);
}
Yes, this is a "Run" button that sends the message and it executes correctly if called only a single time.
|
|
|
|
|
AndrewG1231 wrote: ...it executes correctly if called only a single time. What happens when the handler is called a second time?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
When it is called the second time I encounter some code for checking the state of the instrument my program is controlling. You can see the whole block of code this belongs to in the first post of this thread.
if(!m_inst->StartOperation())
{
MessageBox("The Instrument is Currently in use by another function");
return;
} I thought this indicated that the previous thread wasn't terminating before the message posted again and I tried to use Sleep() to delay the call, but this did not work. I encountered the same situation or the window did not update. I also tried to use the WaitForSingleObject() , but this failed as well.
|
|
|
|
|
Look this over. It's from memory and completly untested so there may be a few small details left out, but it should give you good idea of how primary and secondary threads communicate.
BEGIN_MESSAGE_MAP(CMyDialog, CDiaog)
ON_BN_CLICKED(IDC_BUTTON, OnClick)
END_MESSAGE_MAP()
void CMyDialog::Acq_Data( void )
{
while (1)
{
if (WaitForSingleObject(m_pEventStopRequested->m_hObject, 0U) == WAIT_OBJECT_0)
{
m_pEventThreadDone->SetEvent();
return;
}
else
ADCdbInquire(...);
}
}
UINT CMyDialog::Acq_Data( LPVOID lpVoid )
{
CMyDialog *pDlg = (CMyDialog *) lpVoid;
pDlg->Acq_Data();
return 0;
}
CWinThread *m_pThread = NULL;
CEvent *m_pEventStopRequested = new CEvent(FALSE, TRUE);
CEvent *m_pEventThreadDone = new CEvent(TRUE, TRUE);
void CMyDialog::OnClick()
{
if (WaitForSingleObject(m_pEventThreadDone->m_hObject, 0U) == WAIT_OBJECT_0)
{
m_pEventStopRequested->ResetEvent();
m_pEventThreadDone->ResetEvent();
m_pThread = AfxBeginThread(Acq_Data, this, THREAD_PRIORITY_HIGHEST);
}
}
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
modified 13-Sep-12 9:44am.
|
|
|
|
|
Thanks for the suggestions! However, this piece of code is causing me to get the error, "A nonstatic member reference must be relative to a specific object". I tried to use this-> before m_pEventStopRequested->m_hObject but it did not solve the problem. I have the declaration for the CEvent in the constructor, but I am not sure to what object it is referring.
if (WaitForSingleObject(m_pEventStopRequested->m_hObject, 0U) == WAIT_OBJECT_0)
{
m_pEventThreadDone.SetEvent();
return;
}
Would you happen to know what object it is referring to?
|
|
|
|
|
AndrewG1231 wrote: However, this piece of code is causing me to get the error, "A nonstatic member reference must be relative to a specific object". What is the exact error number?
AndrewG1231 wrote: m_pEventThreadDone.SetEvent(); Should be:
m_pEventThreadDone->SetEvent(); That, and adding a return value, is all I had to do to get the code to compile.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
Here is the exact compiler message of the error.
Dlg_SpectrumAnalyzer.cpp(150): error C2227: left of '->m_hObject' must point to class/struct/union/generic type
1>Dlg_SpectrumAnalyzer.cpp(153): error C2227: left of '->SetEvent' must point to class/struct/union/generic type
1>Dlg_SpectrumAnalyzer.cpp(154): error C2561: 'CDlg_SpectrumAnalyzer::Acq_Data' : function must return a value
However, I implemented you suggestion a bit differently. I did not use void Acq_DAta(void) instead I have incorporated everything in the following member.
UINT CDlg_SpectrumAnalyzer::Acq_Data(LPVOID pParam)
{
CDlg_SpectrumAnalyzer *dlg =(CDlg_SpectrumAnalyzer*) pParam;
HWND hdlg = dlg->GetSafeHwnd();
while(1)
{
if (WaitForSingleObject(m_pEventStopRequested->m_hObject, 0U) == WAIT_OBJECT_0)
{
m_pEventThreadDone->SetEvent();
return;
}
else
{
dlg->m_inst->ADCdbRead(dlg->m_AcqChan,dlg->m_MaxFreq*2,dlg->m_BuffSize,dlg->m_fulldata,dlg->m_halfdata);
while(dlg->m_Continue && ! dlg->m_inst->m_Flags.Halt)
dlg->m_inst->ADCdbInquire(dlg- >m_halfdata,hdlg,MSG_DRAW_SPECTRUM);
dlg->m_inst->ADCdbStop();
dlg->m_inst->EndOperation();
return 1;
}
}
} Here is the button control code.
void CDlg_SpectrumAnalyzer::OnBnClickedRun()
{
if (WaitForSingleObject(m_pEventThreadDone->m_hObject, 0U) == WAIT_OBJECT_0)
{
m_pEventStopRequested->ResetEvent();
m_pEventThreadDone->ResetEvent();
m_pThread = AfxBeginThread(Acq_Data, this, THREAD_PRIORITY_HIGHEST);
}
} Have I done something foolish in the way I was trying to use your suggestion?
modified 13-Sep-12 13:54pm.
|
|
|
|
|
AndrewG1231 wrote:
Have I done something foolish in the way I was trying to use your suggestion? Yes, you are trying to access non-static members from within a static function. Check my code again. Note there are two Acq_Data() functions.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
So, I saw the two Acq_Data() functions and I ran into the following problem with the use of the dlg pointer and the hdlg entry in the ADCdbInquire(...) member which is of the type HWND .
dlg->m_inst->ADCdbRead(dlg->m_AcqChan,dlg->m_MaxFreq*2,dlg->m_BuffSize,dlg->m_fulldata,dlg->m_halfdata);
while(dlg->m_Continue && ! dlg->m_inst->m_Flags.Halt)
dlg->m_inst->ADCdbInquire(dlg->m_halfdata,hdlg,MSG_DRAW_SPECTRUM);
dlg->m_inst->ADCdbStop();
dlg->m_inst->EndOperation();
I tried to combine the call to function and the actual code to enable the process, apparently, this leads to the compiler issue. How can I get the handle? I did try just using HWND hdlg = GetSafeHwnd(); and eliminating all the dlg code which compiled fine but led to the following runtime error.
Unhandled exception at 0x012d2ba9 in WinSTM.exe: 0xC0000005: Access violation reading location 0x000002af.
Your patience has been amazing..usually by now I have received some type of comment about my newbie-ness. Thanks! ![Big Grin | :-D](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_biggrin.gif)
|
|
|
|
|
AndrewG1231 wrote: ...I ran into the following problem with the use of the dlg pointer... From whence comes the dlg pointer?
AndrewG1231 wrote: which compiled fine but led to the following runtime error.
Unhandled exception at 0x012d2ba9 in WinSTM.exe: 0xC0000005: Access violation reading location 0x000002af. Sounds like a NULL pointer. Use the debugger, set a breakpoint on the call to ADCdbRead() , and check the values of dlg , m_inst , and m_Flags .
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
Hey, it has been a little while, but your suggestions were very helpful in diagnosing my problem. I am currently working on a solution, but wanted to share my thanks. ![Smile | :)](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_smile.gif)
|
|
|
|
|
Glad you're progressing.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
Actually, SendMessage is synchronous... so if it's threaded, it really should be a PostMessage , otherwise you'll tie up the worker thread waiting on the GUI update.
|
|
|
|
|
I m reading some data from device whose length I dont know at compile so I made a structure as of following type to store data
struct MessagData
{
DWORD dwSize;
BYTE byData[1];
}
(as far as I know byData[1] declares a byte array of variable length, but I dont know where it allocates memory for the same)
and I m taking a pointer to a variable of this type structure as shown below
MessagData* pMessagData = new MessagData;
suppose data read from device is stored in some variable mentioned below:
char chReadData[100];
after filling the read data which is of variable length i am copying the same data to byData variable of this pointer as shown below:
memcpy(pMessagDat->byData,
chReadData,
pMessagDat->dwSize);
[Length of read data is always less than 100]
upto here it is working fine but when i am trying to do following thing application gave some error of heap corruption even though i m deallocating in the same application in which i have created it.
Please someone help me......
|
|
|
|
|
Rahul from Poona wrote: as far as I know byData[1] declares a byte array of variable length
BYTE byData[1]; declares an array of BYTEs, of length 1.
C/C++ does not have variable length arrays.
|
|
|
|
|
Chris Losinger wrote: C/C++ does not have variable length arrays.
However that type of structure is a very common idiom in C and probably in C++ in certainly situations.
And in common usage the array size would be determined at run time - thus dynamic by most definitions.
|
|
|
|
|
jschell wrote:
And in common usage the array size would be determined at run time - thus dynamic by most definitions.
well, it would be malloc'd by the programmer. that's not really a dynamic array, that's just malloc.
|
|
|
|
|
Chris Losinger wrote: that's not really a dynamic array, that's just malloc.
No idea what that is supposed to mean.
Perhaps you are referring to a C++ array that sizes itself (at least conceptually) according to the data in it. And of course it does that by dynamically, rather than statically, allocating memory.
|
|
|
|
|
i'm saying that silly array-of-one hack doesn't do it for you. there's nothing 'dynamic' about the array of one. you still have to go and malloc the memory explicitly.
|
|
|
|
|
|
guess what, words have synonyms.
you know, like when you started using the word "dynamic" to describe what i and the OP were calling "variable length"
|
|
|
|
|
Chris Losinger wrote: guess what, words have synonyms.
Guess what... I said "thus dynamic by most definitions".
Chris Losinger wrote: you know, like when you started using the word "dynamic" to describe what i and
the OP were calling "variable length"
My usage of that word is a commonly known term for that. As the links that I posted demonstrate.
|
|
|
|
|