本文介绍了如何在对话框背景中运行线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! Hello Everyone, 我正在处理一个有对话框的应用程序。在对话框的一侧有一个自定义,另一侧有Tab Control。使用自定义控件显示条形图,该条形图根据通过USB端口接收的值连续更新。在Tab控件的其中一个选项卡(子对话框)上我放置了一个滑块控件。 我在中为Bar控件编写了线程OnInitDialog()主对话框的功能。一旦我在子窗口上移动滑块控件,条形控件就会停止更新。在释放滑块控件后,我该怎么做才能再次启动该线程? 我现在无法对GUI进行任何更改。如何从子窗口再次调用线程? 这是创建线程和线程函数的代码。 BOOL CMonitorMediaSensor :: OnInitDialog() { CDialogEx :: OnInitDialog(); SetBackgroundColor(g_DlgColor); // TODO:在这里添加额外的初始化 m_chart.SubclassDlgItem(IDC_CUSTOMSENSORBAR, this ); m_chart.PrepareColors(CNSChartCtrl :: SimpleColors); m_chart.AddValue(m_nSensorValue, 传感器值); m_chart.m_nMode = 0 ; // 创建标签:用于显示校准栏 m_SnapDlg = new CMediaSensorSnapDlg; m_SnapDlg->创建(CMediaSensorSnapDlg :: IDD,& m_SnapTabCtrl); m_SnapTabCtrl.AddTab(m_SnapDlg, Calibrate, 0 ); m_SnapDlg-> m_pSnapTabCtrl =& m_SnapTabCtrl; m_SnapDlg-> m_chart.m_nMode = 1 ; // 创建标签:用于媒体分析。 m_SnapDlgMonitor = new CMediaSensorSnapDlg; m_SnapDlgMonitor-> Create(CMediaSensorSnapDlg :: IDD,& m_SnapTabCtrl); m_SnapTabCtrl.AddTab(m_SnapDlgMonitor, 分析, 1 ); m_SnapDlgMonitor-> m_pSnapTabCtrl =& m_SnapTabCtrl; m_SnapDlgMonitor-> m_chart.m_nMode = 0 ; // 创建标签:用于传感器增益调整 m_SensorGainDlg = new CSensorGain; m_SensorGainDlg->创建(CSensorGain :: IDD,& m_SnapTabCtrl); m_SnapTabCtrl.AddTab(m_SensorGainDlg, 传感器增益, 2 ); m_SnapDlgMonitor-> m_pSnapTabCtrl =& m_SnapTabCtrl; // m_SnapDlgMonitor-> m_chart.m_nMode = 0; m_SnapTabCtrl.SetCurFocus( 1 ); / * 启动媒体传感器线程。线程将命令发送到USB端口以获得当前媒体传感器值。 * / g_portio-> m_bCloseMediaSensorThread = FALSE; m_bCloseSensorMonitorThread = FALSE; m_MediaSensorData = new MEDIASENSORDATA; m_MediaSensorData-> pbCloseThread =& m_bCloseSensorMonitorThread; m_MediaSensorData-> pCS =& g_portio-> m_CS; / * 创建线程。 * / m_hMediaSensor = AfxBeginThread(MonitorMediaSensorProc,( void *)m_MediaSensorData, THREAD_PRIORITY_NORMAL, 0 , 0 ); return TRUE; // 除非您将焦点设置为控件 // EXCEPTION:OCX Property Pages应返回FALSE } UINT MonitorMediaSensorProc(LPVOID pParam) { MEDIASENSORDATA * monData =(MEDIASENSORDATA *)pParam; BOOL * pbCloseThread = monData-> pbCloseThread; CCriticalSection * pcs = monData-> pCS; CSingleLock lock(pcs); char szQueryCommand [ 6 ]; szQueryCommand [ 0 ] = g_cSTX; szQueryCommand [ 1 ] = ' K' ; szQueryCommand [ 2 ] = ' L' ; szQueryCommand [ 3 ] = g_cCR; szQueryCommand [ 4 ] = 0x0A; szQueryCommand [ 5 ] = ' \ 0' ; while ( 1 ) { if (lock.Lock()) { if (g_portio-> m_bCloseMediaSensorThread || * pbCloseThread) { lock.Unlock(); delete (monData); monData = NULL; return 0 ; } / * 向USB端口发送查询命令。 * / g_portio-> SendData(szQueryCommand, 6 ); lock.Unlock(); } 睡眠( 100 ); } return 0 ; } 解决方案 现在它正常工作。 我在孩子身上做错了什么窗口。 Hello Everyone,I am working on an application in which there is a dialog box. On one side of a dialog box there is a custom and on other side there is Tab Control. Am using the custom control for displaying a Bar graph which is updating continuously depending on the values am receiving via USB port. On one of the tabs(child dialog) of Tab control I have placed a slider control.I have written the thread for Bar control in OnInitDialog() function of the main dialog box. As soon as I move the slider control on child window, the bar control stops updating. What should I do to start that thread again after I release the slider control?I can''t make any changes in the GUI now. How can I call the thread again from the child window?Here is the code where am creating the thread and the thread function.BOOL CMonitorMediaSensor::OnInitDialog(){CDialogEx::OnInitDialog(); SetBackgroundColor (g_DlgColor);// TODO: Add extra initialization herem_chart.SubclassDlgItem(IDC_CUSTOMSENSORBAR, this);m_chart.PrepareColors(CNSChartCtrl::SimpleColors);m_chart.AddValue(m_nSensorValue,"Sensor Value");m_chart.m_nMode = 0;// Create Tab: Used to display calibration Barm_SnapDlg = new CMediaSensorSnapDlg;m_SnapDlg->Create(CMediaSensorSnapDlg::IDD, &m_SnapTabCtrl);m_SnapTabCtrl.AddTab(m_SnapDlg, "Calibrate", 0);m_SnapDlg->m_pSnapTabCtrl = &m_SnapTabCtrl;m_SnapDlg->m_chart.m_nMode = 1;// Create Tab: Used for Media Profiling.m_SnapDlgMonitor = new CMediaSensorSnapDlg;m_SnapDlgMonitor->Create(CMediaSensorSnapDlg::IDD, &m_SnapTabCtrl);m_SnapTabCtrl.AddTab(m_SnapDlgMonitor, "Profiling", 1);m_SnapDlgMonitor->m_pSnapTabCtrl = &m_SnapTabCtrl;m_SnapDlgMonitor->m_chart.m_nMode = 0; // Create tab: Used for Sensor Gain Adjustmentm_SensorGainDlg = new CSensorGain;m_SensorGainDlg->Create(CSensorGain::IDD, &m_SnapTabCtrl);m_SnapTabCtrl.AddTab(m_SensorGainDlg, "Sensor Gain", 2);m_SnapDlgMonitor->m_pSnapTabCtrl = &m_SnapTabCtrl;//m_SnapDlgMonitor->m_chart.m_nMode = 0;m_SnapTabCtrl.SetCurFocus (1);/*Start Media sensor thread. Thread sends commands to USB Port to get current media sensor values.*/g_portio->m_bCloseMediaSensorThread = FALSE;m_bCloseSensorMonitorThread = FALSE;m_MediaSensorData = new MEDIASENSORDATA;m_MediaSensorData->pbCloseThread= &m_bCloseSensorMonitorThread;m_MediaSensorData->pCS= &g_portio->m_CS;/* Create thread. */m_hMediaSensor = AfxBeginThread (MonitorMediaSensorProc,(void*)m_MediaSensorData,THREAD_PRIORITY_NORMAL, 0, 0);return TRUE; // return TRUE unless you set the focus to a control// EXCEPTION: OCX Property Pages should return FALSE}UINT MonitorMediaSensorProc (LPVOIDpParam){MEDIASENSORDATA*monData= (MEDIASENSORDATA*) pParam;BOOL*pbCloseThread= monData->pbCloseThread;CCriticalSection*pcs= monData->pCS;CSingleLocklock(pcs);char szQueryCommand[6];szQueryCommand[0] = g_cSTX;szQueryCommand[1] = 'K';szQueryCommand[2] = 'L';szQueryCommand[3] = g_cCR;szQueryCommand[4] = 0x0A;szQueryCommand[5] = '\0';while (1){if(lock.Lock ()){if(g_portio->m_bCloseMediaSensorThread || *pbCloseThread){lock.Unlock ();delete(monData);monData = NULL;return 0;}/* Send query command to USB Port. */g_portio->SendData (szQueryCommand, 6);lock.Unlock ();}Sleep (100);}return 0;} 解决方案 Its working properly now.I was doing something wrong on the child window. 这篇关于如何在对话框背景中运行线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-11 11:52