Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

Access CNiGraph from worker thread?

I am trying to plot my data in my worker thread. I did as follows (I also attaching an example project), but I got run time error.

I am using Measurement Studio 6.0 and Visual Studio 6.0 SP#5.

I read some answers on this problem. Maybe, CoInitialize() is hint. However, I cannot understand how to use this function. How I can access?

Please read my example:

UINT ThreadProc(LPVOID lParam);
......
BOOL CMyThreadDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.
......
// TODO: Add extra initialization here
AfxBeginThread(ThreadProc,(LPVOID)this);

return TRUE; // return TRUE unless you set the focus to a control
}

UINT ThreadProc(LPVOID lParam)
{
C
MyThreadDlg* pDlg = (CMyThreadDlg*)lParam;
CNiReal64Vector tData;
CNiMath::SineWave(tData,200,1.0);
pDlg->m_graph.PlotY(tData);

return (UINT)0;
}
0 Kudos
Message 1 of 2
(3,024 Views)
I believe you are correct about CoInitialize being the problem. To see what error you are actually getting, change your code as follows:

UINT ThreadProc(LPVOID lParam)
{
CoInitialize(NULL);
try
{
CMyThreadDlg* pDlg = (CMyThreadDlg*)lParam;
CNiReal64Vector tData;
CNiMath::SineWave(tData,200,1.0);
pDlg->m_graph.PlotY(tData);
}
catch (CException *e)
{
e->ReportError();
e->Delete();
}
CoUnitialize();
return (UINT)0;
}

You should see a MessageBox that tells you that you probably need to call CoInitialize(). To do that, change your code as follows:

UINT ThreadProc(LPVOID lParam)
{
try
{
CMyThreadDlg* pDlg = (CMyThreadDlg*)lParam;
CNiReal64Vector tData;
CNiMath::SineWave(tData,200,1.0);
pDlg->m_graph.PlotY(tData);
}
catch (CException *e
)
{
e->ReportError();
e->Delete();
}

return (UINT)0;
}
0 Kudos
Message 2 of 2
(3,024 Views)