Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.


C++
BOOL CMonitorMediaSensor::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	

        SetBackgroundColor (g_DlgColor);


	// TODO:  Add extra initialization here

	m_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 Bar
	m_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 Adjustment
	m_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 (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;
			}
			
			/* Send query command to USB Port. */
			g_portio->SendData (szQueryCommand, 6);
	
			lock.Unlock ();
		}

		Sleep (100);
	}

	return 0;
}
Posted
Updated 26-May-13 22:55pm
v4
Comments
Richard MacCutchan 27-May-13 4:31am    
You need to show some of your code and explain how the thread is getting terminated; it's impossible to guess what your program is doing.
Fresher16 27-May-13 4:55am    
I have updated my question... Please have a look.
Santhosh G_ 27-May-13 11:46am    
MonitorMediaSensorProc will remain until the following condition satisfies.
if(g_portio->m_bCloseMediaSensorThread || *pbCloseThread)
In my suggestion MonitorMediaSensorProc should not return until the all SendData() operation completed. Restarting the thread to send each command is not a good design.

1 solution

Its working properly now.
I was doing something wrong on the child window.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900