Click here to Skip to main content
15,912,977 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Threading Interview Question Pin
Cedric Moonen31-Aug-06 4:53
Cedric Moonen31-Aug-06 4:53 
GeneralRe: Threading Interview Question Pin
toxcct31-Aug-06 5:29
toxcct31-Aug-06 5:29 
AnswerRe: Clarification Pin
Nawal K Gupta31-Aug-06 19:40
Nawal K Gupta31-Aug-06 19:40 
QuestionGenerating GUID Pin
<color>Aljechin 31-Aug-06 4:30
<color>Aljechin 31-Aug-06 4:30 
AnswerRe: Generating GUID Pin
ovidiucucu31-Aug-06 4:52
ovidiucucu31-Aug-06 4:52 
QuestionProblem with select() with multiple sockets [modified] Pin
DevendraC31-Aug-06 4:07
DevendraC31-Aug-06 4:07 
AnswerRe: Problem with select() with multiple sockets Pin
toxcct31-Aug-06 4:17
toxcct31-Aug-06 4:17 
QuestionCAnimateCtrl crashes when going out of scope Pin
almc31-Aug-06 3:46
almc31-Aug-06 3:46 
Hi there!

I’m trying to use the CAnimateCtrl in a CDialog based class so that I can use it as a kind of CWaitCursor thing, that will show a “Please wait…” message an plays a AVI.
Now, since I would also like to use the “XP Theme”, I’ve defined a “manifest” as a resource, and as a result the AVI file will not play.
In order to make it work, I tried to use a thread as the one in the MSDN sample (CAnimateCtrl::CAnimateCtrl in MFC Library Reference).
The problem is when the CAnimateCtrl is being destroyed it takes too long (?), and it also makes the application crash. The MSDN says that "... If you create the CAnimateCtrl object on the stack, it is destroyed automatically."

Below you can see the most relevant (I think) parts of the code. Can someone please tell me what am I doing wrong?

Thanks in advance.


CWorkingDlg::CWorkingDlg( LPCSTR szLabel, bool bProgress, int nMaxProgress, bool bShowCancel, UINT nIdAVI, CWnd* pParent)
: CDialog(CWorkingDlg::IDD, pParent)
{
m_bCancelState = false;
m_dwThreadId = 0;
m_hThreadAvi = NULL;
m_hEventAvi = NULL;

m_bCancel = bShowCancel;
m_bProgress = bProgress;
m_nMaxProgress = nMaxProgress;

CString strLabel(szLabel);
if(strLabel.IsEmpty())
strLabel = "A processar. Aguarde por favor...";
m_strLabel = strLabel;

if (nIdAVI==0)
nIdAVI=IDR_XX_AVI_RODAS;

m_nAVI=nIdAVI;

EnableModal(pParent);
if(!Create(CWorkingDlg::IDD, pParent))
AfxThrowUserException();

ShowWindow(SW_SHOW);
DispatchMsg();
}

CWorkingDlg::~CWorkingDlg()
{
if (m_hThreadAvi)
{
::PostThreadMessage(m_dwThreadId, WM_STOPCLIP, 0L, 0L);
::WaitForSingleObject(m_hEventAvi, INFINITE);
m_hThreadAvi=NULL;
}

if (m_hEventAvi)
{
::CloseHandle(m_hEventAvi);
m_hEventAvi=NULL;
}

DisableModal(GetParent());
if (GetSafeHwnd())
DestroyWindow();
}

void CWorkingDlg::DispatchMsg()
{
MSG msg;
DWORD dwi=GetTickCount();
while (::PeekMessage(&msg, NULL, 0L, 0L, PM_NOREMOVE))
{
if (::GetMessage(&msg,NULL,0L,0L))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
else
return;

if ((GetTickCount()-dwi)>2000) // 2s
return;
}
}

UINT CWorkingDlg::MyClipThreadProc(LPVOID pParam)
{
AviInfo *pInfo=(AviInfo *)pParam;
CWnd* pParentWnd = CWnd::FromHandle(pInfo->hWndParent);

CRect rc(10,10,100,100);
CWnd *pAvi=pParentWnd->GetDlgItem(IDC_XX_AVI);
if (pAvi)
{
pAvi->GetWindowRect(&rc);
pParentWnd->ScreenToClient(&rc);
}

UINT nIdAVI=pInfo->nIdAvi;
HANDLE hEvent=pInfo->hEvento;
delete pInfo;
pInfo=NULL;

// Create the animation control.
try
{
CAnimateCtrl cAnimCtrl;
if (!cAnimCtrl.Create(WS_CHILD|WS_VISIBLE|ACS_CENTER,
rc, pParentWnd, IDC_XX_AVI))
{
::SetEvent(hEvent);
return false;
}

// Open the AVI file.
if (!cAnimCtrl.Open(nIdAVI))
{
::SetEvent(hEvent);
return false;
}

// Pump message from the queue until the stop play message is received.
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) && (msg.message != WM_STOPCLIP))
{
switch (msg.message)
{
// Start playing from the first frame to the last,
// continuously repeating.
case WM_PLAYCLIP:
if (!cAnimCtrl.Play(0, -1, -1))
return false;
break;
}

::TranslateMessage(&msg);
::DispatchMessage(&msg);
}

cAnimCtrl.Stop();
cAnimCtrl.Close();
}
catch(...)
{
_com_error e(::GetLastError());
TRACE("\n--> Error: %s", e.ErrorMessage());
}

::SetEvent(hEvent);
return true;
}

BOOL CWorkingDlg::OnInitDialog()
{
CDialog::OnInitDialog();

CenterWindow();

m_hEventAvi=::CreateEvent(NULL,TRUE,FALSE,"Event_AVI");

AviInfo *pInfo=new AviInfo;
pInfo->hWndParent=GetSafeHwnd();
pInfo->nIdAvi=m_nAVI;
pInfo->hEvento=m_hEventAvi;

CWinThread *pThread=AfxBeginThread(MyClipThreadProc, pInfo);
if (pThread)
{
m_hThreadAvi=pThread->m_hThread;
m_dwThreadId=pThread->m_nThreadID;
::PostThreadMessage(m_dwThreadId, WM_PLAYCLIP, 0L, 0L);
}
else
delete pInfo;

return TRUE;
}


ALMC
AnswerRe: CAnimateCtrl crashes when going out of scope Pin
Cedric Moonen31-Aug-06 3:54
Cedric Moonen31-Aug-06 3:54 
GeneralRe: CAnimateCtrl crashes when going out of scope Pin
almc31-Aug-06 3:59
almc31-Aug-06 3:59 
GeneralRe: CAnimateCtrl crashes when going out of scope Pin
David Crow31-Aug-06 7:34
David Crow31-Aug-06 7:34 
GeneralRe: CAnimateCtrl crashes when going out of scope Pin
almc31-Aug-06 23:05
almc31-Aug-06 23:05 
QuestionBackGround Bitmap Pin
radhika2831-Aug-06 3:45
radhika2831-Aug-06 3:45 
AnswerRe: BackGround Bitmap Pin
_AnsHUMAN_ 31-Aug-06 4:03
_AnsHUMAN_ 31-Aug-06 4:03 
AnswerRe: BackGround Bitmap Pin
Hamid_RT31-Aug-06 7:31
Hamid_RT31-Aug-06 7:31 
QuestionQuestion for Foreign Langauge Speakers Pin
Joel Holdsworth31-Aug-06 3:36
Joel Holdsworth31-Aug-06 3:36 
AnswerRe: Question for Foreign Langauge Speakers Pin
toxcct31-Aug-06 3:44
toxcct31-Aug-06 3:44 
AnswerRe: Question for Foreign Langauge Speakers Pin
Rage31-Aug-06 3:45
professionalRage31-Aug-06 3:45 
GeneralRe: Question for Foreign Langauge Speakers Pin
toxcct31-Aug-06 4:09
toxcct31-Aug-06 4:09 
Questionshow text on next line in an edit box Pin
Kiran Pinjala31-Aug-06 2:44
Kiran Pinjala31-Aug-06 2:44 
AnswerRe: show text on next line in an edit box Pin
_AnsHUMAN_ 31-Aug-06 2:51
_AnsHUMAN_ 31-Aug-06 2:51 
QuestionMouse button down Pin
majco33331-Aug-06 2:14
majco33331-Aug-06 2:14 
AnswerRe: Mouse button down Pin
toxcct31-Aug-06 2:26
toxcct31-Aug-06 2:26 
AnswerRe: Mouse button down Pin
IlanTal31-Aug-06 2:26
IlanTal31-Aug-06 2:26 
AnswerRe: Mouse button down Pin
_AnsHUMAN_ 31-Aug-06 2:41
_AnsHUMAN_ 31-Aug-06 2:41 

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.