Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

Error on calling ChartXY from a thread

Hello,
 
my program crashes (Unhandled exception) when I call ChartXY from a thread I start, and does what I expect when I call ChartXY from outside the thread.
 
I have a class with a member function

void

CGraphDlg::DisplayDataPoint(double x, double y, bool FirstDataPoint)

{

m_UpperGraph.ChartXY(x, y);

m_LowerGraph.ChartXY(x, y);

}

When I call this function from my View class

m_pGraphDlg->DisplayDataPoint(1, 2, 1);

everything works fine. When I start a thread and call this function

(pO

->m_pGraphDlg)->DisplayDataPoint(2, 3, 0);

I get an "Unhandled Exception".

When I trace into the DisplayDataPoint function to check the value of the pointer to the CNiGraph object m_UpperGraph, it's the same in both function calls.

 

Your help would be appreciated!

Best regrads,

Kurt

0 Kudos
Message 1 of 4
(3,838 Views)
You need to call CoInitialize(NULL) as the very first function in your new thread to initialize COM. Make sure you call CoUninitialize() before you exit the thread. One of the golden rules of Win32 programming is that you cannot access UI elements from a thread other than the one that created it. We added some additional functionality to the CNiGraph to be able to do this without too much extra code. Plus MFC initializes COM for you in the main thread and this graph is a COM component. But for any other thread that you create, you need to manually initialize COM for that thread by calling CoInitialize(). Otherwise you get crashes like the one you described.

I hope this helps

Message Edited by bilalD on 07-06-2005 10:14 AM

Bilal Durrani
NI
0 Kudos
Message 2 of 4
(3,835 Views)
Hi,
 
thanks a lot for your answer. Now it works!! Probably I would have never found this bug without your help.
Best regards,
Kurt
0 Kudos
Message 3 of 4
(3,830 Views)

I have the same problem.but, Now it has been solved!!

I would have never found this bug without your help.

Thank you very much.

Sincerely.

0 Kudos
Message 4 of 4
(3,354 Views)