// WndThread.cpp : implementation file
//

#include "stdafx.h"
#include "CgDemoBy5000asi.h"
#include "WndThread.h"
#include "MainFrm.h"
#include "CardDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CWndThread

IMPLEMENT_DYNCREATE(CWndThread, CWinThread)

CWndThread::CWndThread()
{
	m_uCardNum = -1;
}

CWndThread::~CWndThread()
{
}

BOOL CWndThread::InitInstance()
{
	CCardDlg dlg;
	if(dlg.DoModal() != IDOK)
	{	return false;
	}
	if (!Seekfit_By5000asiplay_OpenCard(By5000ASIPLAY_MODE_CG, dlg.m_iCardNum))
		return FALSE;

	//The event name must be unique
	//HANDLE hEvent;
	//if(!Seekfit_InitialFieldEvent(dlg.m_strEventName.GetBuffer(), hEvent, dlg.m_iCardNum))
	//{	Seekfit_CloseCard(dlg.m_iCardNum);
	//	AfxMessageBox(_T("Initial Event Error!!"), MB_OK | MB_ICONINFORMATION);
	//	return FALSE;
	//}

	CMainFrame* pFrame = new CMainFrame;
	m_pMainWnd = pFrame;
	m_uCardNum = dlg.m_iCardNum;
	m_strEventName = dlg.m_strEventName;
	pFrame->m_uCardNum = dlg.m_iCardNum;
	pFrame->m_strEventName = dlg.m_strEventName;

	// create and load the frame with its resources
	pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL);

	// The one and only window has been initialized, so show and update it.
	pFrame->ShowWindow(SW_SHOW);
	pFrame->UpdateWindow();
	CString strTitle;
	strTitle.Format(_T("CgDemoBy5000Asi Card %d"), m_uCardNum);
	pFrame->SetWindowText(strTitle);

	return TRUE;	
}

int CWndThread::ExitInstance()
{
	if(m_uCardNum != -1)
	{
//		Seekfit_CloseEvent(NULL, m_uCardNum);
		Seekfit_By5000asiplay_CloseCard(m_uCardNum);          //close the card
	}
	CWinThread * pThread = this;
	BOOL bRet = CWinThread::ExitInstance();

	FreeWndThread(pThread);
	
	return bRet;
}

BEGIN_MESSAGE_MAP(CWndThread, CWinThread)
	//{{AFX_MSG_MAP(CWndThread)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWndThread message handlers
