Code:
UINT C_Control::StartCryptoPortThread( LPVOID pParam )
{
char* buffer;
int size = 0;
C_RS_422_Port* pObject = ( C_RS_422_Port*)pParam;
if (pObject == NULL )
return 1; // if pObject is not valid
else
{
//C_Control::GetOutboundMessage(buffer,size);
pObject->Send_Crypto_Key(1); // do something with 'pObject'
return 0; // thread completed successfully
}
}
C_Control::C_Control()
{
PortThread[3]= AfxBeginThread(StartCryptoPortThread, &Port[3],THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
PortThreadCrypto = PortThread[3];
b_Port_Status[3]=FALSE;
b_Port_Open[3] = FALSE;
}
void Crypto_Dlg::OnBnClickedCryptoPortAct()
{
// TODO: Add your control notification handler code here
CString sString;
CButton* pButton;
const bool Active = TRUE;
if(!Executive.b_Port_Open[3] && ((CButton* )GetDlgItem(IDC_Crypto_Port_Act))->GetCheck() )
{
Executive.OpenPort(3, 7);
}
pButton = (CButton*)GetDlgItem(IDC_Crypto_Port_Act);
pButton->GetState();
if (pButton->GetCheck())
{
pButton->SetCheck(0);
pButton->SetState(0);
GetDlgItemText(IDC_Crypto_Port_Act,sString);
if (!sString.Compare("Activate"))
{
Executive.Set_Activate(4, Active);
SetDlgItemText(IDC_Crypto_Port_Act,"Deactivate");
}
else
{
Executive.Set_Activate(4,!Active);
SetDlgItemText(IDC_Crypto_Port_Act,"Activate");
}
Executive.On_Crypto_Apply_Event();
// m_b_Crypto_Port_Act = TRUE;
}
}
void C_Control::On_Crypto_Apply_Event()
{
// this function controls the thread starting and stopping.
// the threads are synchronized on a start and a stop event.
// the thread functions that this functions controls are
// those that write data to the ports and to the log files
// if port is currently inactive - start and set timing
if (!b_Port_Status[3])
{
goto startports;
}
else // port already active
{
Activate_Crypto_Port(4, FALSE);
goto startports;
}
startports:
Activate_Crypto_Port(4, TRUE);
Port[3].StartTransmission.SetEvent();
}
bool C_Control::Activate_Crypto_Port(int PortNumber, bool ActiveYN)
{
// this function starts or suspends a port thread depending
// on the state of ActiveYN and opens the log files the first
// time a port is activated
static int Times_Through[4] = {0,0,0,0};
DWORD SuspendCount;
if (ActiveYN) //activate
{
if (Times_Through[PortNumber-1]==0)
{
// Open the log files only the first time a write event happens
// Port[PortNumber-1].OpenLogFiles(FileNames[PortNumber-1]);
Times_Through[PortNumber-1]=1;
}
SuspendCount=PortThread[PortNumber-1]->ResumeThread();
if (SuspendCount < 2)
{
b_Port_Status[PortNumber-1] = ActiveYN;
return(TRUE);
}
else
{
b_Port_Status[PortNumber-1] = FALSE;
AfxMessageBox("Unable to resume Crypto thread", MB_OK|MB_ICONEXCLAMATION , 0);
return(FALSE);
}
}
else
{
SuspendCount=PortThread[PortNumber-1]->SuspendThread();
if (SuspendCount >= 0)
{
b_Port_Status[PortNumber-1] = ActiveYN;
return(TRUE);
}
else
{
b_Port_Status[PortNumber-1] = FALSE;
AfxMessageBox("Unable to suspend Crypto thread", MB_OK|MB_ICONEXCLAMATION , 0);
return(FALSE);
}
}
}
UINT C_RS_422_Port::Send_Crypto_Key(int key)
{
DWORD aword = ::WaitForMultipleObjects(2,EventHandles,FALSE,INFINITE);
//more code based on aword
}
As you can see, I haven't made many significant changes.