MFC 线程使用

CWinThread*  m_pthread; //多线程对象

 

struct param//参数传递结构体
{
int id;
TASK t[20];
};

 

CNB400Dlg::CNB400Dlg(CWnd* pParent /*=NULL*/)//构造函数
: CDialogEx(CNB400Dlg::IDD, pParent)
, m_time(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pthread = NULL;

}

 

CNB400Dlg::~CNB400Dlg()//析构函数
{
if (m_pthread)
{
DWORD exit;
GetExitCodeThread(m_pthread->m_hThread, &exit);
TerminateThread(m_pthread->m_hThread, exit);
}

}

主线程中启动:

param * pA = new param;

pA->t[count].TID = tid;
pA->t[count].SID = FindSid(i, j);

// m_dati->PName
CString pro;
pro.Format(m_dati->PName);

pA->t[count].TProName = pro;

pA->t[count].RID1 = FRid1(pro);
pA->t[count].RID2 = FRid2(pro);

pA->t[count].Snum = FSnum(pro);
pA->t[count].Rnum1 = FRnum1(pro);
pA->t[count].Rnum2 = FRnum2(pro);

pA->t[count].status = S_M2;
pA->t[count].type = T_BIC;
pA->t[count].dilute = 0;
pA->t[count].dilutenum = 0;

pA->t[count].emg = Findemg(i, j);

pA->t[count].data.primaryempty = 0;
pA->t[count].data.secondaryempty = 0;

count++;
tid++;

CWinThread* m_pthread = AfxBeginThread((AFX_THREADPROC)MyThreadProc, (LPVOID)pA, THREAD_PRIORITY_NORMAL, 0, 0, NULL);
CWinThread* m_pthread1 = AfxBeginThread((AFX_THREADPROC)MyThreadPro1, NULL, THREAD_PRIORITY_NORMAL, 0, 0, NULL);

 

工作线程:

(1)在.h文件中的申明:

static UINT MyThreadProc(LPVOID pParam);

2)在.CPP中的实现

UINT CNB400Dlg::MyThreadProc(LPVOID pParam)//不断获取申请的任务,启动测试
{

while (1)
{
Sleep(1000);//1ms为单位
if (count > 1)
{
m_mutex.Lock();
//获取主线程传递的参数值
for (int i = 1; i < count; i++)//count值比任务数多1
{
param* bt = (param*)pParam;

t[i].TID = (bt->t[i]).TID;
t[i].SID = (bt->t[i]).SID;
t[i].RID1 = (bt->t[i]).RID1;
t[i].RID2 = (bt->t[i]).RID2;
t[i].Snum = (bt->t[i]).Snum;
t[i].Rnum1 = (bt->t[i]).Rnum1;
t[i].Rnum2 = (bt->t[i]).Rnum2;
t[i].status = (bt->t[i]).status;
t[i].type = (bt->t[i]).type;
t[i].dilute = (bt->t[i]).dilute;
t[i].dilutenum = (bt->t[i]).dilutenum;
t[i].emg = (bt->t[i]).emg;
t[i].data.primaryempty = (bt->t[i]).data.primaryempty;
t[i].data.secondaryempty = (bt->t[i]).data.secondaryempty;

worker.NewTask(t[i]);

CString str, r1id;
//int->cstring
r1id.Format("%d", t[i].RID1);

str.Format("样本编号为\"%d\"的样本添加完成!", t[i].SID);
str = str + "\n" + "当前样本试剂R1编号为:" + r1id + "\n";
AfxMessageBox(str);

}

}

worker.SetSpeed(SPEED_1);
worker.StartWork();

STATUS status;
status = worker.GetStatus(t[1].TID);
CString ss;
ss.Format("1、样本初始申请状态在第 %d 阶段", (int)status);
AfxMessageBox(ss);
m_mutex.Unlock();
break;

}

return 0;
}

 

posted on 2015-11-16 10:52  MILUMI  阅读(295)  评论(0编辑  收藏  举报

导航