Click here to Skip to main content
16,004,647 members

Comments by lephanhung (Top 3 by date)

lephanhung 3-Apr-14 7:59am View    
I try to use PostThreadMessage, but i am not sure that my structure right or wrong. In header file:

#define MM_PCB WM_USER+4
DWORD threadProc(LPVOID lParam);
class CPredOTSDlg : public CDialogEx
{
// Construction
public:
CPredOTSDlg(CWnd* pParent = NULL); // standard constructor
LRESULT nOnThreadQuitFunction(WPARAM wParam, LPARAM lParam);
}

In cpp file:

/*****************************************************************
Global Variables
*****************************************************************/
DWORD m_idThread;
bool
m_bStopTracking, /* flag that tells the thread to stop tracking */
m_bIsTracking; /* flag that specifies if we are tracking */

UINT FillTrackingTable(LPVOID pParam); /* FillTrackingTable thread declaration */
void CALLBACK TimeProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
CCriticalSection cs;
CPredOTSDlg* obj = (CPredOTSDlg*) dwUser;
//obj->MMTimerHandler(wTimerID);
cs.Lock();
//obj->nGetSystemTransformData(0,0); /* Get tumor position */
//obj->strf();
PostThreadMessage(m_idThread, MM_PCB,0,0);
cs.Unlock();
}
ON_MESSAGE(MM_PCB,&CPredOTSDlg::nOnThreadQuitFunction)

DWORD threadProc(LPVOID lParam)
{

MSG msg;
CPredOTSDlg *cs = new CPredOTSDlg;

while(GetMessage(&msg,0,0,0))
{
if(msg.message == MM_PCB)
{
cs->nGetSystemTransformData(0,0);
cs->strf();
}
DispatchMessage(&msg);
}
delete(cs);
return 0;
}

LRESULT CPredOTSDlg::nOnThreadQuitFunction(WPARAM wParam, LPARAM lParam)
{
return LRESULT(0);
}

But have an error : PredOTS.exe the 0x5a69ca31 (msvcr100d.dll) unhandled exceptions. 0xC0000005: 0xcdce92f9 position caused an access violation while recording.

The breakpoint shows these lines. These Lines from the library .h that i include when using the device.

memset(m_szCommand, 0, sizeof(m_szCommand));
sprintf( m_szCommand, "TX %04X", nReplyMode );
lephanhung 2-Apr-14 8:21am View    
Yes! I think because the synchronization between CAllBACK and GUI. When I try to remove the code relative to GUI, it run without error. But when I add some code like Updatedata(), it will have error.
This is my code in header file:
// PredOTSDlg.h : header file
//

#pragma once

#define WM_FILL_LIST WM_USER+1
#define WM_COM_PORT_TO WM_USER+2
#define WM_CAL_PRE WM_USER+3
#define MODE_PRE_INIT 0x01
#define MODE_INIT 0x02
#define MODE_TRACKING 0x03
#define MODE_ACTIVATED 0x04

#include "CommandHandling.h" /* included the command handling header */
#include "XStructure.h" /* included the structure */
#include "PreFunc.h" /* included functions for pre-calculate */
#include "LMS.h" /* included Least Mean Squares Prediction */
#include "afxcmn.h"
#include <fstream>
#include "mmsystem.h"

#define MMTIMER
// CPredOTSDlg dialog
class CPredOTSDlg : public CDialogEx
{
// Construction
public:
CPredOTSDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_PREDOTS_DIALOG };
static void CALLBACK TimeProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);
afx_msg void MMTimerHandler(UINT nIDEvent); /* mm_timer */

protected:
virtual BOOL DestroyWindow();
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

// LMS prediction parameters
int m_iM;
int m_iDelta;
double m_dmux;
double m_dmuy;
double m_dmuz;
VW WeightVector;/* Declare Weight Vector*/

// Implementation
protected:
HICON m_hIcon;
CMenu * pMainMenu;

// mmtimer

UINT mmtimer; /* mm_timer periodic*/
UINT resolution; /* mm_timer resolution*/

// Generated message map functions

virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();

// Send message made by user
afx_msg LONG nGetSystemTransformData( UINT wParam, LONG lParam );
afx_msg LONG nComPortTimeout( UINT wParam, LONG lParam );
afx_msg LONG nCalculatePrediction( UINT wParam, LONG lParam );

// OTS system controller
afx_msg void OnBnClickedResetSystem();
afx_msg void OnBnClickedInitializeSystem();
afx_msg void OnBnClickedActivatePorts();
afx_msg void OnBnClickedTrackingBut();

// Declare some helpful function
afx_msg void Doclick(); /* Training button click will do in this function */
afx_msg void strf(); /* Shorten function for collecting and predicting */
afx_msg void outTxtFile(void); /* Export to TXT file */
afx_msg void outlistlog(void); /* Export to list log */
afx_msg void PredictionX(void); /* Predict px,py,pz */
afx_msg void SetWeightVector(void); /* Set initialization of Weight Vector */
afx_msg void DeleteDynamicMemory(void); /* Delete Memory used to allocate */

//afx_msg void MMTimerHandler(UINT nIDEvent); /* mm_timer */
afx_msg void StopTimer(); /* Stop mm_timer or on_timer */

// LMS prediction controller
afx_msg void OnBnClickedTrainingData();
afx_msg void OnBnClickedPredictionData();
afx_msg void OnBnClickedCalculateRms();

// Menu commander
afx_msg void OnSettingsComportsettings();
afx_msg void OnSettingsRomfilesettings();
afx_msg void OnSystemInitializesystem();
afx_msg void OnSystemActivatehandles();
afx_msg void OnSystemStarttracking();
afx_msg void OnSystemStoptracking();
afx_msg void OnViewSystemproperties();
afx_msg void OnViewDiagnosticalerts();
afx_msg void OnOptionsIlluminatorfiringrate();
afx_msg void OnOptionsProgramoptions();

// System
afx_msg void OnFileExit();
afx_msg void OnTimer(UINT_PTR nIDEvent);

// OTS
CCommandHandling *pCommandHandling; /* point to the command handling class */
bool m_bResetHardware; /* reset hardware variable */
bool m_bWireless; /* uses the wireless compatible settings */
bool m_bSystemInitialized; /* is the system initialized */
bool m_bPortsActivated; /* are ports activated */
int m_nCOMPort; /* the current com port number */
CString m_szSystemMode;
BOOL m_bUseEulerAngles;

/**********
lephanhung 1-Apr-14 21:17pm View    
Thank you for your help! This is a part of wincore.cpp
905 #ifdef _DEBUG
906 void CWnd::AssertValid() const
907 {
908 if (m_hWnd == NULL)
909 return; // null (unattached) windows are valid
910
911 // check for special wnd??? values
912 ASSERT(HWND_TOP == NULL); // same as desktop
913 if (m_hWnd == HWND_BOTTOM)
914 ASSERT(this == &CWnd::wndBottom);
915 else if (m_hWnd == HWND_TOPMOST)
916 ASSERT(this == &CWnd::wndTopMost);
917 else if (m_hWnd == HWND_NOTOPMOST)
918 ASSERT(this == &CWnd::wndNoTopMost);
919 else
920 {
921 // should be a normal window
922 ASSERT(::IsWindow(m_hWnd));
923
924 // should also be in the permanent or temporary handle map
925 CHandleMap* pMap = afxMapHWND();
926 ASSERT(pMap != NULL);
927
928 CObject* p=NULL;
929 if(pMap)
930 {
931 ASSERT( (p = pMap->LookupPermanent(m_hWnd)) != NULL ||
932 (p = pMap->LookupTemporary(m_hWnd)) != NULL);
933 }
934 ASSERT((CWnd*)p == this); // must be us
935
936 // Note: if either of the above asserts fire and you are
937 // writing a multithreaded application, it is likely that
938 // you have passed a C++ object from one thread to another
939 // and have used that object in a way that was not intended.
940 // (only simple inline wrapper functions should be used)
941 //
942 // In general, CWnd objects should be passed by HWND from
943 // one thread to another. The receiving thread can wrap
944 // the HWND with a CWnd object by using CWnd::FromHandle.
945 //
946 // It is dangerous to pass C++ objects from one thread to
947 // another, unless the objects are designed to be used in
948 // such a manner.
949 }
950 }