Click here to Skip to main content
16,010,268 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionCWnd with API? Pin
Dragos197920023-Dec-02 5:55
Dragos197920023-Dec-02 5:55 
AnswerRe: CWnd with API? Pin
Anonymous3-Dec-02 6:05
Anonymous3-Dec-02 6:05 
AnswerRe: CWnd with API? Pin
roel_3-Dec-02 6:18
roel_3-Dec-02 6:18 
AnswerRe: CWnd with API? Pin
dabs3-Dec-02 8:32
dabs3-Dec-02 8:32 
GeneralNeed help in firing event from second thread using shared variable!! Pin
bahruddina3-Dec-02 5:24
bahruddina3-Dec-02 5:24 
GeneralRe: Need help in firing event from second thread using shared variable!! Pin
Andreas Saurwein3-Dec-02 6:10
Andreas Saurwein3-Dec-02 6:10 
GeneralRe: Need help in firing event from second thread using shared variable!! Pin
Scott H. Settlemier3-Dec-02 6:32
Scott H. Settlemier3-Dec-02 6:32 
GeneralRe: Need help in firing event from second thread using shared variable!! Pin
bahruddina3-Dec-02 18:14
bahruddina3-Dec-02 18:14 
Thanks for the reply. Smile | :)

Actually I want to use that variable. Below is my real source code:

struct ScheduledDevice<br />
{<br />
	CComBSTR RefID;<br />
	CComBSTR Name;<br />
	DATE StartTime;<br />
	DATE EndTime;<br />
	int Status;<br />
	long Command;<br />
};<br />
<br />
typedef map<BSTR, ScheduledDevice> SCHEDULELIST;<br />
SCHEDULELIST m_Schedule;<br />
SCHEDULELIST m_SendToOPC;


At main thread I receive input from client:

HRESULT CMonitorDeviceThread::Add(BSTR DeviceName, DATE StartTime, DATE EndsTime, BSTR RefID)<br />
{<br />
	ScheduledDevice obj;<br />
<br />
	obj.Name = DeviceName;<br />
	obj.StartTime = StartTime;<br />
	obj.EndTime = EndsTime;<br />
	obj.RefID = RefID;<br />
	obj.Status = bookOpenStatus;<br />
<br />
	EnterCriticalSection(&cs);		<br />
		m_Schedule[obj.RefID] = obj;<br />
	LeaveCriticalSection(&cs);<br />
<br />
	return S_OK;<br />
}


And at second thread the code will continously check and intiate the event:

while ( m_IsRun == TRUE )<br />
{<br />
        //1. read and delete m_Schedule map object<br />
        //2. save certain value at m_Schedule to m_SendToOPC<br />
        //3. Initiate and fire event, make value m_SendToOPC as event parameters<br />
        <big>Command();</big><br />
        Sleep(10000); //loop intervals<br />
}<br />
<br />
void CMonitorDeviceThread::Command(void)<br />
{<br />
	ScheduledDevice obj, OPCDev;<br />
	SCHEDULELIST::iterator m_Iterator;<br />
	bool IsErase = false;<br />
 <br />
	EnterCriticalSection(&cs);<br />
	<br />
		m_SendToOPC.clear();<br />
		<br />
		//START: loop trough all the schedule command/record in map<br />
		for ( m_Iterator = m_Schedule.begin(); m_Iterator != m_Schedule.end(); )<br />
		{	<br />
			 			<br />
			SYSTEMTIME dtStartTime, dtEndTime, stCurDate;<br />
				<br />
			VariantTimeToSystemTime( (*m_Iterator).second.StartTime, &dtStartTime );<br />
			VariantTimeToSystemTime( (*m_Iterator).second.EndTime, &dtEndTime );<br />
			GetLocalTime(&stCurDate);<br />
<br />
			CHighTime bookStartTime( dtStartTime ); <br />
			CHighTime bookEndTime( dtEndTime ); <br />
			CHighTime curTime( stCurDate ); <br />
<br />
			// start time<br />
			CHighTimeSpan sDiffStartTime = bookStartTime - curTime;<br />
			//end time<br />
			CHighTimeSpan sDiffEndTime = bookEndTime - curTime;<br />
			<br />
			// get difference in minute for start time<br />
			long diffStart = sDiffStartTime.GetTotalMinutes(); <br />
			// get difference in minute for end time<br />
			long diffEnd = sDiffEndTime.GetTotalMinutes(); <br />
<br />
			//<br />
			//START time<br />
			//<br />
			//if time = current time or we late 5 minute; and status is open<br />
			if ( ( diffStart == 0 || ( diffStart >= -5 && diffStart <= -1 ) ) && (*m_Iterator).second.Status == bookOpenStatus ) {<br />
			<br />
				//save to event map<br />
				OPCDev = (*m_Iterator).second;<br />
				OPCDev.Command = 1;<br />
				m_SendToOPC[OPCDev.RefID] = OPCDev;<br />
<br />
				//mark as executed<br />
				(*m_Iterator).second.Status = bookExecuteStatus;<br />
<br />
			}<br />
<br />
			//<br />
<br />
			//<br />
			//END time<br />
			//<br />
			//if time = current time or we late 10 minute; and status is open<br />
			if ( ( diffEnd == 0 || ( diffEnd >= -10 && diffEnd <= -1 ) ) && (*m_Iterator).second.Status == bookExecuteStatus ) {<br />
			<br />
				OPCDev = (*m_Iterator).second;	<br />
				OPCDev.Command = 0;<br />
				m_SendToOPC[OPCDev.RefID] = OPCDev;<br />
<br />
				//mark as executed<br />
				(*m_Iterator).second.Status = bookCloseStatus;<br />
			<br />
			//remove obselete record..if any<br />
			} else if ( diffEnd <= -11 ){<br />
<br />
				//remove booking record			<br />
				m_Iterator=m_Schedule.erase(m_Iterator);<br />
				IsErase = true; //mark erase operation occured,<br />
								// so we dont need to execute ++m_Schedule<br />
			}<br />
<br />
			//determine if we need to move to the next element;		<br />
			if ( !IsErase ){<br />
				++m_Iterator;<br />
				IsErase = false;<br />
			}<br />
<br />
			//<br />
				 <br />
		}//for ( m_Iterator = m_Schedule.begin(); m_Iterator != m_Schedule.end(); m_Iterator++ )<br />
		//END: loop trough al the schedule command/record<br />
<br />
	LeaveCriticalSection(&cs);<br />
<br />
	//precess the event<br />
	<big>ProcessEvent();</big><br />
}<br />
<br />
void CMonitorDeviceThread::ProcessEvent()<br />
{<br />
<br />
	if (m_SendToOPC.size() > 0 ) {<br />
<br />
		long recCount = m_SendToOPC.size();<br />
		long i=0;<br />
		SAFEARRAYBOUND bounds = {recCount, 0};<br />
<br />
		VARIANT pItems;<br />
		VariantInit(&pItems);<br />
		pItems.vt = VT_ARRAY | VT_BSTR | VT_BYREF;<br />
<br />
		VARIANT pCommands;<br />
		VariantInit(&pCommands);<br />
		pCommands.vt = VT_ARRAY | VT_I4 | VT_BYREF;<br />
<br />
		SAFEARRAY* psaItems = NULL;<br />
		BSTR *strItems;<br />
		psaItems = SafeArrayCreate(VT_BSTR, 1, &bounds);<br />
<br />
		SAFEARRAY* psaCommands = NULL;<br />
		long *lngCommands;<br />
		psaCommands = SafeArrayCreate(VT_I4, 1, &bounds);<br />
<br />
		//fill the array<br />
		SafeArrayAccessData(psaItems, (void**)&strItems);<br />
		SafeArrayAccessData(psaCommands, (void**)&lngCommands);<br />
<br />
		for ( m_OPCIterator = m_SendToOPC.begin(); m_OPCIterator != m_SendToOPC.end(); m_OPCIterator++)<br />
		{<br />
			strItems[i] = (*m_OPCIterator).second.Name;<br />
			lngCommands[i] = (*m_OPCIterator).second.Command;<br />
			i++;<br />
		}<br />
		//for ( m_OPCIterator = m_SendToOPC.begin(); m_OPCIterator != m_SendToOPC.end(); m_OPCIterator++)<br />
<br />
		SafeArrayUnaccessData(psaItems);<br />
		SafeArrayUnaccessData(psaCommands);<br />
<br />
		//set the value to be sent<br />
		pItems.pparray = &psaItems;<br />
		pCommands.pparray = &psaCommands;<br />
<br />
		//fire event<br />
		<big>Fire_NotifyAllConnectedClients(pItems, pCommands, i);</big><br />
<br />
		SafeArrayDestroy(psaItems);<br />
		SafeArrayDestroy(psaCommands);<br />
<br />
	}<br />
	//if ( m_SendToOPC.size() > 0 ) {<br />
<br />
}

Here is ATL wizard generated CP event file :

VOID Fire_NotifyAllConnectedClients(VARIANT Items, VARIANT Command, LONG saje)<br />
	{<br />
		T* pT = static_cast<T*>(this);<br />
		int nConnectionIndex;<br />
		CComVariant* pvars = new CComVariant[3];<br />
		int nConnections = m_vec.GetSize();<br />
		<br />
		for (nConnectionIndex = 0; nConnectionIndex < nConnections; nConnectionIndex++)<br />
		{<br />
			pT->Lock();<br />
			CComPtr<IUnknown> sp = m_vec.GetAt(nConnectionIndex);<br />
			pT->Unlock();<br />
			IDispatch* pDispatch = reinterpret_cast<IDispatch*>(sp.p);<br />
			if (pDispatch != NULL)<br />
			{<br />
				pvars[2] = Items;<br />
				pvars[1] = Command;<br />
				pvars[0] = saje;<br />
				DISPPARAMS disp = { pvars, NULL, 3, 0 };<br />
				pDispatch->Invoke(0x1, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, NULL, NULL, NULL);<br />
			}<br />
		}<br />
		delete[] pvars;<br />
	<br />
	}


Again thank a lot Smile | :)

Regards,
Newbie
GeneralRe: Need help in firing event from second thread using shared variable!! Pin
bahruddina4-Dec-02 4:07
bahruddina4-Dec-02 4:07 
GeneralDirectX 9.0 >>PUBLIC<< SDK Pin
Addabis3-Dec-02 5:20
Addabis3-Dec-02 5:20 
GeneralRe: DirectX 9.0 >>PUBLIC<< SDK Pin
Paul M Watt3-Dec-02 5:48
mentorPaul M Watt3-Dec-02 5:48 
GeneralRe: DirectX 9.0 >>PUBLIC<< SDK Pin
ChrisDM20-Dec-02 17:50
ChrisDM20-Dec-02 17:50 
GeneralADO related Pin
Shamoon3-Dec-02 5:05
Shamoon3-Dec-02 5:05 
GeneralRe: ADO related Pin
Michael P Butler3-Dec-02 5:19
Michael P Butler3-Dec-02 5:19 
GeneralNeed help for code Pin
Boss713-Dec-02 4:58
Boss713-Dec-02 4:58 
GeneralRe: Need help for code Pin
Rage3-Dec-02 5:05
professionalRage3-Dec-02 5:05 
QuestionHow to programmatically Virus scan a file? Pin
Avad3-Dec-02 4:56
Avad3-Dec-02 4:56 
AnswerRe: How to programmatically Virus scan a file? Pin
roel_3-Dec-02 6:20
roel_3-Dec-02 6:20 
GeneralTreeView & Drag & Drop Pin
graffitisampler3-Dec-02 4:48
sussgraffitisampler3-Dec-02 4:48 
GeneralDelete Button problem Pin
Ruca3-Dec-02 4:43
Ruca3-Dec-02 4:43 
GeneralRe: Delete Button problem Pin
Rage3-Dec-02 5:01
professionalRage3-Dec-02 5:01 
GeneralRe: Delete Button problem Pin
Ruca3-Dec-02 5:58
Ruca3-Dec-02 5:58 
GeneralCToolBar - adding text to buttons Pin
Christoffer A. Andersen3-Dec-02 4:35
Christoffer A. Andersen3-Dec-02 4:35 
QuestionListbox, scrollbar vanish on resize? Pin
Anonymous3-Dec-02 2:44
Anonymous3-Dec-02 2:44 
GeneralExtract Icon from Exe Pin
AnTri3-Dec-02 2:40
AnTri3-Dec-02 2:40 

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.