TCanvas Update Problem

Hello,

I’m running root 4.02 on windows and trying to simply draw a histogram in an MFC application in another window. i have done this several times before by running a TApplication object in the background, but seem to be having trouble updating this particular application to do so and am guessing that it’s something so simple that i’m just overlooking it again and again. basically, the application will either run and just not draw anything (although the canvas will pop up with the appropriate title) or if i change it to try to read a histogram from a file then it will crash when it calls canvas->Update(). The project is a bit too convoluted to post in its entirety, but hopefully the example code from the dialog section is enough to get an idea of what is going on:

/* copied from an example from bellenot*/
VOID CALLBACK MyTimerProc(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime)
{
   static bool inloop = false;
   if(inloop) return;
   inloop = true;
   gApplication->StartIdleing();
   gSystem->InnerLoop();
   gApplication->StopIdleing();
   inloop = false;
} 

// set up the background TApplication:
CEgViewerDlg::CEgViewerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEgViewerDlg::IDD, pParent)
{
   ........
   m_app = new TApplication("App",0,0);
}
BOOL CEgViewerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
        ........
	SetTimer(1, 5, (TIMERPROC) MyTimerProc);
}

//button to draw histogram
void CEgViewerDlg::OnDraw() {
	
	UpdateData( TRUE );
	m_dc = new TCanvas("can","the canvas"); // member variable 
	m_dc->Clear();	
	m_dc->cd();

        // just a test histogram to see if it works
	TH1F* h1 = new TH1F("h1","test histogram",50,0,50);
	h1->SetDirectory(0);
	for(int i=0;i<50;i++){
         h1-Fill(i,rand());
	}
	h1-Draw("hist");//
	UpdateData(FALSE);
	m_dc-Update();
	

}

Any ideas would be greatly appreciated!

-Reese

Hi,

Well, I don’t see major obvious problem with your code…
At least it works with current CVS version.
Anyway, I will try with version 4.02 asap and let you know.
BTW, if you switch to a Root version > 5.12, you will be able to embed a TCanvas into a MFC Window…
(see : root.cern.ch/root/Version51000.news.html MFC & ROOT)

Cheers,
Bertrand.

Hi,

It works also with Root version 4.02.00 and MSVC 7.1 (see attached screen capture)

Cheers,
Bertrand.


hmm, yeah i did the same thing and it worked. forgot to mention that. something strange is going on somewhere else in the code i think, even though the canvas is never referenced again outside of that function. anyway, i think it may just be a case where i need to go through the whole thing step by step else just rewrite it. thanks for the help though.

nb. would update to 5.02 eventually, but trying to get it working with something else that is in 4.02 first!

Note that our production version is not 5.02 but 5.14

Rene

If you wish, you can send me the full project (zipped) privately (bertrand.bellenot@cern.ch), so I could take a look and maybe find the problem…

Cheers,
Bertrand.

Bertrand -

thank you for the offer. think i will just resign myself to slugging through it removing bits step by step for now in the hopes that i can figure out what’s bugging it up. may take you up on your offer though if i get too too frustrated!

cheers - Reese