Article ID: 196026
Article Last Modified on 5/22/2003
STDMETHODIMP CMyAtl::InitTask(long number)
{
HANDLE hThrd;
DWORD tid;
m_number = number;
if((hThrd = CreateThread(
0,
0,
(LPTHREAD_START_ROUTINE)justDoIt,
(void *)this,
0,
&tid)) == NULL)
{
//error handling here
}
CloseHandle(hThrd);
return S_OK;
}
DWORD WINAPI justDoIt(LPVOID lpParameter)
{
CMyAtl *myAtl = (CMyAtl*)lpParameter;
long result;
for (int i = 1; i <= myAtl->m_number; ++i)
result = i * 2;
myAtl->Fire_TaskFinished(result);
return 0;
}
DWORD WINAPI justDoIt(LPVOID lpParameter);
Option Explicit
Private WithEvents vbATL As MYATLDLLLib.MyAtl
Private Sub Command1_Click()
vbATL.InitTask 11111
End Sub
Private Sub Form_Load()
Set vbATL = New MYATLDLLLib.MyAtl
End Sub
Private Sub vbATL_TaskFinished(ByVal result As Long)
MsgBox result
End Sub
#include <atlwin.h>
after the line:
#define _MYATL_H
in the MyAtl.h file.
#define WM_TASK_FINISH (WM_USER + 101)
before the line:
class ATL_NO_VTABLE CMyAtl :
in the MyAtl.h file.
public CWindowImpl<CMyAtl>,
right after the line:
class ATL_NO_VTABLE CMyAtl :
in the file MyAtl.h.
public:
DECLARE_WND_CLASS("MyAtl")
BEGIN_MSG_MAP(CMyAtl)
MESSAGE_HANDLER(WM_TASK_FINISH, OnTaskFinished)
END_MSG_MAP()
public:
LRESULT OnTaskFinished(UINT uMsg, WPARAM wParam,
LPARAM lParam, BOOL& bHandled)
{
Fire_TaskFinished((long)wParam);
return 0;
}
HRESULT FinalConstruct()
{
RECT rect;
rect.left=0;
rect.right=100;
rect.top=0;
rect.bottom=100;
HWND hwnd = Create( NULL, rect, "MyAtlWindow", WS_POPUP);
if (hwnd)
return S_OK;
else
return HRESULT_FROM_WIN32(GetLastError());
}
void FinalRelease()
{
if (m_hWnd != NULL)
DestroyWindow();
}
myAtl->Fire_TaskFinished(result);
in the function
DWORD WINAPI justDoIt(LPVOID lpParameter)
in the file MyAtl.cpp, to be:
myAtl->PostMessage(WM_TASK_FINISH,result,0);
157437 FILE: Fireev.exe Fires Events from a Second Thread
280512 SAMPLE: ATLCPImplMT Encapsulates ATL Event Firing Across COM
Keywords: kbthread kbprb kbcode kbactivexevents KB196026