Click here to Skip to main content
15,917,473 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Help me Pin
Prakash Nadar25-Apr-04 18:29
Prakash Nadar25-Apr-04 18:29 
GeneralView Scrolling Pin
Reunion25-Apr-04 17:18
Reunion25-Apr-04 17:18 
GeneralRe: View Scrolling Pin
Prakash Nadar25-Apr-04 18:51
Prakash Nadar25-Apr-04 18:51 
QuestionHow to run an exe from memory? Pin
KevinZhaoShK25-Apr-04 16:25
KevinZhaoShK25-Apr-04 16:25 
AnswerRe: How to run an exe from memory? Pin
David Crow26-Apr-04 2:42
David Crow26-Apr-04 2:42 
QuestionHow can add and change myself data to exe file?? Pin
fftz25-Apr-04 16:19
fftz25-Apr-04 16:19 
AnswerRe: How can add and change myself data to exe file?? Pin
grigsoft25-Apr-04 19:52
grigsoft25-Apr-04 19:52 
GeneralWorker Thread problem Pin
nanukos25-Apr-04 15:09
nanukos25-Apr-04 15:09 
I would like to use the OO approach to threads that Derel Lakin uses on his article posted on this board. It subclasses CWinThread to create a class that allows multiple instances to effectively use the same thread function, safely.

I have derived a class (CMyThread)from CWinThread following his approach. This will be my base class, with all the basic members (events) and methods to start and stop a thread, from where I derived all my other classes that need to be executed on a diffent thtead.

I have a main thread on my appliaction, CMainThread derived from CMyThread. This class will act as a socket sever, it will bind to a port waiting for incoming connections. For each new conection it will create a new object of the class CWorkerThread (also derived from CMyThread) that has the functionality needed to process my business logic transactions.It basically listens to the client socket and when it has a full command it executes the transaction bussines logic. When it finish with the transaction it goes back to listen to the client socket for more incomming commads.

For debugging purpose I limit my application to only one object of the class CWorkerThread (only on client conects to it). I have the one client sending always the same command.

The problem I see is that the transaction that I have the one client sending over and over, always same exact one, varies a lot on the time it takes to execute. Some times it gets done on 10 mili seconds (what I am expecting) and other times it may go up to a couple of minutes. I'm logging plenty of degub statements to a text file to debug the problem, all lines on the log file have a time stamp, I see that the only delays on executing the transaction are when I make ADO calls to my oracle database. I have tried the ADO calls (Select statements) using TOAD (enteprise manager) and they always take around 10 mili seconds.

The virtual function Execute() of the class CWorkerThread is the entry point of execution for the thread that I want executing the bussines logic of the transaction. From this virtual function I will access the class data members and call the class methods. Can this be a problem?

Can any one give me any ideas on what is going on? Sorry for the long message. Here are the declaration of the three clases


class CMyThread : public CWinThread
{
DECLARE_DYNCREATE(CMyThread)
protected:
CMyThread(); // protected constructor used by dynamic creation

CMyThread(AFX_THREADPROC pfnThreadProc);


// Attributes
public:
//CInternalException m_SettlementException;

protected:
CLogFile *m_pLogFile; //Pointer to the logfile object that all derived threads will use
CString m_sThreadName; //Name of the thread
CString m_sFunctionName;//Name of the function being executed
CString m_sTmp;

CString m_sErrorMessage;

//Commands that the thread responds to
HANDLE m_hEventStart;
HANDLE m_hEventStop;

//Statuses of the thread
HANDLE m_hEventWorking;
HANDLE m_hEventDone;
HANDLE m_hEventReady;
HANDLE m_hEventQuit;


//this is a pointer to an array of handles to all commands (events) that this thread supports
HANDLE m_Handles[2];

long m_nThreadTimeOut; //Number of seconds the thread waits before it times out
double m_dStartTime;
double m_dNumTransDone;
double m_dTransactionTime;
double m_dTotTime;


// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyThread)
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
//}}AFX_VIRTUAL

void SetThreadName(CString Value) {m_sThreadName = Value;};
void SetLogFile(CLogFile *Value) {m_pLogFile = Value;};
CLogFile *GetLogFile(void) {return m_pLogFile;};

void WriteToLog(CString Value);
void WriteToLog(int DebugLevel, CString Value);
void TerminateThread();
CString GetThreadName(){return m_sThreadName;};

//Member functions to send commands to the thread
void StartProcessing(void);
void StopProcessing(void);


//Member functions to get the status of the thread
HANDLE *GetDoneStatus(){return &m_hEventDone;};
HANDLE *GetWorkingStatus(){return &m_hEventWorking;};
HANDLE *GetReadyStatus(){return &m_hEventReady;};
HANDLE *GetQuitStatus(){return &m_hEventQuit;};

void SetThreadTimeOut(long Value) { m_nThreadTimeOut = Value;};

void RaiseInternalException(CInternalException &, CString);


// Implementation
static UINT ThreadFunc( LPVOID lpParam);
protected:
virtual ~CMyThread();
virtual UINT Execute();
void DealWithTheError();

void CatchComError(_com_error &, CString );

// Generated message map functions
//{{AFX_MSG(CMyThread)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
};


class CMainThread : public CMyThread
{
private:
//Socket stuff
WSADATA m_wsaData; SOCKET m_sServerSocket;
sockaddr_in m_saddrSocketAddres;
SOCKET m_sClientSocket;

vector<cworkerthread *=""> m_vWThreadList;
CWorkerThread *m_pWorkerThread;
HANDLE *lpQuitHandles;


int m_nMaxTransactions;
int m_nNumActiveThreads;
long m_nWaitForCommands;
long m_nWTCommandTimeOut;
long m_nSTCommandTimeOut;

long m_nServerPort;


//ADO objects for the CRS database
CString m_sCRSConnString;
CString m_sORCConnString;
CString m_sFailOverList;



public:
CMainThread();
CMainThread(AFX_THREADPROC pfnThreadProc);
virtual ~CMainThread();

long InitMainThread();
int GetNumberOfActiveThreads() {return m_nNumActiveThreads;};
void SetCRSConnString(CString Value){m_sCRSConnString = Value;};
void SetORCConnString(CString Value){m_sORCConnString = Value;};
void SetFailOverList(CString Value) {m_sFailOverList = Value;};
void SetWaitForCommands(long Value) {m_nWaitForCommands = Value;};
void SetWTTimeOut(long Value) {m_nWTCommandTimeOut = Value;};
void SetSTTimeOut(long Value) {m_nSTCommandTimeOut = Value;};
void SetServerPort(long Value) {m_nServerPort = Value;};


BOOL InitInstance();
int ExitInstance();

protected:
//It will execute all the logic of this thread
virtual UINT Execute();
private:
long CreateWorkingThread();
long CheckStopCommand();
long FatalError(CString, long);
long GetOutStoppingWorkerThreads();
long GetArrayOfStopHandles( );
};

class CWorkerThread : public CMyThread
{
private:
SOCKET m_ClientSocket;
CString m_sMessage;
char m_cMessageByte;
CString m_sFunctionName;

CString m_sORCConnString;
CString m_sFailOverList;

CADOConnect *m_pORCConn;
CADOCommand *m_pORCCommand;
CADORecordset *m_pORCRs;
_ParameterPtr m_pORCParameter;
FieldPtr m_pORCFld;

//XML document object
IXMLDOMDocument2Ptr m_objDoc;

_bstr_t m_bstrCommand, m_bstrStationNo, m_bstrStoreCode, m_bstrCheckAmt, m_bstrCheckDate, m_bstrCheckNo;
_bstr_t m_bstrClaimNo, m_bstrFM, m_bstrMR, m_bstrDL, m_bstrPhone, m_bstrZip;



public:
CWorkerThread ();
CWorkerThread (AFX_THREADPROC pfnThreadProc);
virtual ~CWorkerThread ();

BOOL InitInstance();
int ExitInstance();

void SetSocket(SOCKET Value) {m_ClientSocket = Value;};


void SetOracleConnectionString(CString Value) {m_sORCConnString = Value;};
void SetOracleFailOvers(CString Value) {m_sFailOverList = Value;};

protected:
//It will execute all the logic of this thread
virtual UINT Execute();
private:
long ParseMessage();
long InitXML();
long LoadXMLRequest();
long GetXMLValue(_bstr_t *Value, _bstr_t NodeName);

long FindSingleMatch();
long FindGroupMatch();

long InitializeADOObjects();

};
GeneralRe: Worker Thread problem Pin
Antony M Kancidrowski26-Apr-04 2:07
Antony M Kancidrowski26-Apr-04 2:07 
GeneralRe: Worker Thread problem Pin
nanukos26-Apr-04 4:30
nanukos26-Apr-04 4:30 
GeneralRe: Worker Thread problem Pin
Antony M Kancidrowski26-Apr-04 11:41
Antony M Kancidrowski26-Apr-04 11:41 
GeneralCTreeView assert when clicking in CEdit Pin
newbeetoc25-Apr-04 14:02
newbeetoc25-Apr-04 14:02 
GeneralRe: CTreeView assert when clicking in CEdit Pin
Mike Dimmick26-Apr-04 0:19
Mike Dimmick26-Apr-04 0:19 
Generalpassing parameters to main in debugger Pin
toxcct25-Apr-04 10:55
toxcct25-Apr-04 10:55 
GeneralRe: passing parameters to main in debugger Pin
f6425-Apr-04 11:22
f6425-Apr-04 11:22 
GeneralRe: passing parameters to main in debugger Pin
toxcct25-Apr-04 11:24
toxcct25-Apr-04 11:24 
GeneralCalling api functions with C++ Pin
soul.ripper25-Apr-04 10:24
soul.ripper25-Apr-04 10:24 
GeneralRe: Calling api functions with C++ Pin
toxcct25-Apr-04 11:07
toxcct25-Apr-04 11:07 
GeneralRe: Calling api functions with C++ Pin
soul.ripper25-Apr-04 11:26
soul.ripper25-Apr-04 11:26 
GeneralRe: Calling api functions with C++ Pin
f6425-Apr-04 11:48
f6425-Apr-04 11:48 
GeneralRe: Calling api functions with C++ Pin
toxcct25-Apr-04 11:53
toxcct25-Apr-04 11:53 
Questionlonger paths than MAX_PATH ? Pin
name_or_alias25-Apr-04 10:13
name_or_alias25-Apr-04 10:13 
AnswerRe: longer paths than MAX_PATH ? Pin
peterchen25-Apr-04 10:19
peterchen25-Apr-04 10:19 
GeneralRe: longer paths than MAX_PATH ? Pin
name_or_alias25-Apr-04 11:04
name_or_alias25-Apr-04 11:04 
GeneralRe: longer paths than MAX_PATH ? Pin
peterchen25-Apr-04 11:11
peterchen25-Apr-04 11:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.