Page 1 of 1

how to create teechart in a subthread in mfc

Posted: Mon Apr 27, 2015 8:42 am
by 6919081
I want to use teechart to draw powersprectrum in my application which is based on MFC. In my subthread,it occurs the error "access violation at address in module 'TeeChart5.ocx'",i don't know how to solve this problem.Anyone can tell me how to solve this problem,please. My code as follow:

Code: Select all

UINT CEEG_ONLINEDlg::CalculateThread(LPVOID pParam)
{
	CEEG_ONLINEDlg *p_Calculate = (CEEG_ONLINEDlg*)pParam;	//p_Calculate可以调用类中的成员函数;
	while (TRUE)
	{
		//float bFil[] = { 0.7403, 0.0005, -3.6991, -0.001, 7.3955, -0, -7.3955, 0.001, 3.6991, -0.0005, -0.7403 };//IIR,10or,Fs=100,bp(0.5,3,48,50)
		//float aFil[] = { 1, 0.1211, -4.3896, -0.4116, 7.7502, 0.5284, -6.8754, -0.3033, 3.0632, 0.0656, -0.5481 };
		if (bThreadControl)
		{
			memcpy(p_Calculate->pCalculateBuffer, m_pBuffer, sizeof(short) * nChannelNum * NPT_PER_READ);	//将采集到的数据复制到计算线程的临时存储区;
			p_Calculate->Calculate();
			//::SendMessage(AfxGetApp()->m_pMainWnd->m_hWnd, WM_MYMESSAGE, 0, 0);
			
			
			CSeries series1 = ((CSeries)(p_Calculate->m_MyChart).Series(0));
			series1.Clear();
			for (int i = 0; i != p_Calculate->len; ++i)
			{
				series1.AddXY(i * p_Calculate->pwScale, p_Calculate->ampR[1][i], NULL, RGB(255, 0, 0));
			}
			SetEvent(g_hEventForCollect);
		}
		else
		{
			break;
		}
	}
	return 0;
}

Re: how to create teechart in a subthread in mfc

Posted: Mon Apr 27, 2015 10:14 am
by yeray
Hello Richard,

I don't see anything wrong in the code if the series is created and the ampR[1] array is created and populated.
Just note TeeChart isn't thread safe so you should take care of it preventing the chart to be repainted from one thread and modified from another thread.

If you still find problems with it, please arrange a simple example project we can run as-is to reproduce the problem here.

Re: how to create teechart in a subthread in mfc

Posted: Mon Apr 27, 2015 12:32 pm
by 6919081
Yeray wrote:Hello Richard,

I don't see anything wrong in the code if the series is created and the ampR[1] array is created and populated.
Just note TeeChart isn't thread safe so you should take care of it preventing the chart to be repainted from one thread and modified from another thread.

If you still find problems with it, please arrange a simple example project we can run as-is to reproduce the problem here.

yes,i still find problems with it, how about create teechart dynamically in my subthread.please tell me how to create teechart in a subthread.By the way, My version is Teechart Pro version5. Thinks

Re: how to create teechart in a subthread in mfc

Posted: Mon Apr 27, 2015 2:50 pm
by yeray
Hello,
Richard wrote:how about create teechart dynamically in my subthread.please tell me how to create teechart in a subthread.By the way, My version is Teechart Pro version5.
I attached a test application for you here. Have you tried it?
http://www.teechart.net/support/viewtop ... 984#p68984

Re: how to create teechart in a subthread in mfc

Posted: Tue Apr 28, 2015 1:01 am
by 6919081
Yeray wrote:Hello,
Richard wrote:how about create teechart dynamically in my subthread.please tell me how to create teechart in a subthread.By the way, My version is Teechart Pro version5.
I attached a test application for you here. Have you tried it?
http://www.teechart.net/support/viewtop ... 984#p68984
yes,i have tried it,but when i input proj ID the application shows error message,i don't why??

Re: how to create teechart in a subthread in mfc

Posted: Tue Apr 28, 2015 8:28 am
by yeray
Hello Richard,
Richard wrote:yes,i have tried it,but when i input proj ID the application shows error message,i don't why??
The application is to show how to create a chart at runtime. If you type "TeeChart.TChart.10" it should add a TChart on the form. Doesn't it for you?