|
problem with zoom by using thread in mfc |
Posted by pyduan on Sep-13-2017 09:25 |
|
HI~ We have a problem.
I want to use Realtime Chart with Zooming in MFC, the demo use SetTimer to realize realtime, but our project use thread like this:
AfxBeginThread(Test, this);
UINT Test(LPVOID lpParam)
{
CClearanceTestDlg* dlg = (CClearanceTestDlg*)lpParam;
while (true)
{
dlg->DrawProfile(&dlg->m_ChartViewer);
Sleep(200);
}
return 0;
}
void CClearanceTestDlg::DrawProfile(CChartViewer* viewer)
{
const char *imageMap = 0;
//... load data
//... config CChartViewer
viewer->syncLinearAxisWithViewPort("x", c->xAxis());
viewer->syncLinearAxisWithViewPort("y", c->yAxis());
delete viewer->getChart();
viewer->setChart(c);
//include tool tip for the chart
imageMap = c->getHTMLImageMap("clickable", "", "title='[{dataSetName}] X = {x}, Y = {value}'");
viewer->setImageMap(imageMap);
}
This program will throw exception when zooming, can you give me some suggestion?
Thanks! |
Re: problem with zoom by using thread in mfc |
Posted by Peter Kwan on Sep-13-2017 16:36 |
|
Hi pyduan,
For your information, there are several multi-threading realtime chart examples in:
http://www.advsofteng.com/tutorials/extra.html
For your case, please note the followings:
(a) The CDialog and the CChartViewer can only be used after MFC has initialized them and connected them to the dialog resource. That means your thread should be started only after OnInitDialog.
(b) Also, in MFC, and in fact in all GUI frameworks I know of, you cannot use any GUI controls in a new thread. It is because a control will be called by the operating system to handle various events (such as mouse move, resize, paint, etc..). These events are unpredictable. The operating system will call the control in the "GUI" thread. If you use the control in another thread, there will be random concurrent access, and the system may crash. This applies not just to CChartViewer but to any MFC control.
The multi-threading examples mentioned above provide one possible method to use multi-threading with GUI controls, which makes sure code that uses the GUI control (such as CChartViewer) only runs in the GUI thread.
Hope this can help.
Regards
Peter Kwan |
Re: problem with zoom by using thread in mfc |
Posted by pyduan on Sep-19-2017 16:40 |
|
Dear Peter:
Thanks for your reply!
Regards |
|