MFC编程单文档应用程序,编译没有问题,一运行就报错说“访问违例”,调试发现父窗口丢了“hWnd=???”,我用createthread生成了一个线程,是不是跟这个有关啊?不知道怎么解决希望大家给点建议。
代码如下:
// RunDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SPLC.h"
#include "RunDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRunDlg dialog
HWND hWnd;
CWinThread* pMyThread;
bool isrun;
CString cmd;
CString m_cmd;
CString cmd_t;
CString cmd_i;
UINT i;
//////////////////////////////////////////////////////////////////////////
.......................
/////////////////////////////////////////////////////////////////////////////
// CRunDlg message handlers
void CRunDlg::OnRadio1()
{
// TODO: Add your control notification handler code here
if(1==m_r1.GetCheck())
{
m_r1.SetCheck(0);
I0=0;
}
else
{
m_r1.SetCheck(1);
I0=1;
}
}
void CRunDlg::OnCancelrun()
{
// TODO: Add your control notification handler code here
CWinApp *app;
app=AfxGetApp();
isrun=TRUE;
WaitForSingleObject(pMyThread->m_hThread,INFINITE);
CloseHandle(pMyThread);
}
void CRunDlg::OnStart()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_CANCELRUN)->EnableWindow(TRUE);
GetDlgItem(IDC_START)->EnableWindow(FALSE);
isrun=FALSE;
pMyThread=AfxBeginThread(RunProc,hWnd);
}
UINT RunProc( LPVOID pParam )
{
CRunDlg * pRunProc=(CRunDlg *) pParam;
CSPLCApp *app=(CSPLCApp*)AfxGetApp();
cmd=app->cmdtemp;
int len;
int len_t,len_tc;
while(!isrun)
{
len=m_cmd.GetLength();
if(!len)
m_cmd=cmd;
while(len>0)
{
cmd_t=m_cmd.Left(m_cmd.Find('\r'));
len_t=cmd_t.GetLength()+1;
len=len-len_t;
m_cmd=m_cmd.Right(len);
cmd_i=cmd_t.Left(cmd_t.Find(' '));
i=pRunProc->DealwithCmd(cmd_i);
len_tc=cmd_i.GetLength()+1;
cmd_t.Delete(0,len_tc);
pRunProc->Compile();
}
}
return 0;
}
UINT CRunDlg::DealwithCmd(CString str)
{
str.MakeUpper();
if(str=="LD")
return 1;
else
if(str=="OUT")
return 4;
else
{ MessageBox("不识别");
return 0;
}
}
void CRunDlg::Compile()
{
switch(i)
{
case 1:OnLD();break;
case 2:break;
case 3:break;
case 4:OnOUT();break;
}
}
void CRunDlg::OnLD()
{
cmd_t.Remove('.');
int t;
t=atoi(cmd_t);
switch(t)
{
case 0:
value_t=I0;break; ///////////////////调试到此报错!!!
case 1:
break;
case 2:break;
default:
break;
}
}
void CRunDlg::OnOUT()
{
}
BOOL CRunDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
I0=0;
value_t=0;
m_O0=0;
GetDlgItem(IDC_CANCELRUN)->EnableWindow(FALSE);
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}