Strange behavior of TGProgressBar within a Thread

Dear root-community,

I am trying to program a little status frame within the user can see the cpu load, used ram etc.
Getting the values and also the SetPosition and ShowPosition Members are running in an own thread.
The Frame does most of the time as it should.
But sometimes there seems to be an graphical error and also I have seen several times that the “Usr-Load” text appears in the status bar of the “CPU-Load”. When I want to show also the percentage within the status bar then I get strange hieroglyphs when the error occurs.

This is the actual code of the class method which runs in its own thread:

void *StatusDisplay :: Refresh(void *Ptr)
	StatusDisplay *p = (StatusDisplay *)Ptr;
	MemInfo_t memInfo;
	CpuInfo_t cpuInfo;

	while(p -> fOpen)
		//std :: cout << Hist -> GetEntries() - nEntries << std :: endl;
		Double_t CountRate = (p -> Hist -> GetEntries() - p -> nEntries) * 2;
		p -> tCountRate -> Form("%.0lf 1/s", CountRate);
		p -> lCountRate -> SetText(p -> tCountRate -> Data());
		p -> nEntries = p -> Hist -> GetEntries();

		gSystem -> GetCpuInfo(&cpuInfo, 400);
		gSystem -> GetMemInfo(&memInfo);
		if(cpuInfo.fTotal >= 0 && cpuInfo.fTotal <= 100)
			p -> pbCPU -> SetPosition(cpuInfo.fTotal);
			p -> pbCPU -> ShowPosition(kTRUE, kFALSE, "CPU-Load");
		if(cpuInfo.fSys >= 0 && cpuInfo.fSys <= 100)
				p -> pbCPUSys -> SetPosition(cpuInfo.fSys);
				p -> pbCPUSys -> ShowPosition(kTRUE, kFALSE, "Sys-Load");
		if(cpuInfo.fUser >= 0 && cpuInfo.fUser <= 100)
			p -> pbCPUUser -> SetPosition(cpuInfo.fUser);
			p -> pbCPUUser -> ShowPosition(kTRUE, kFALSE, "Usr-Load");
		p -> pbRAM -> SetPosition((Float_t)memInfo.fMemUsed);
		p -> pbRAM -> ShowPosition(kTRUE, kFALSE, "Used RAM: %.0f MB");
		gSystem -> Sleep(500);
	return 0;

Could it be that I have done something messy within my thread?
Is there maybe a better way to get such an status frame?

Thanks in advance

Hi Johannes,

Well, hard to tell with so less information… It would be much better with a running piece of code reproducing the problem. But anyway, the GUI is not really thread safe, and the update may occur at any time (assuming you are running on a X11 based system - you forgot to tell which version of ROOT, on which platform…). I would advise to update the GUI elements in the main thread only, if possible, or try to synchronize X11 events, for example by calling gVirtualX->Update(1), or gVirtualX->UpdateWindow(0) in your loop.

Cheers, Bertrand.

Hi Bertrand,

thanks for your fast reply.
I am using root 5.34/05 under Windows and the Microsoft compiler, sorry I forgot to attach these important information.
I will try to implement your suggestions.
If I don’t get it running I will try to attach a minimal code sample.

I now update the Progress Bars with more or less the same Refresh Method but for invoking the method I am using a TTimer.
The problem with the strange behaviour seems to be solved.
But what I dislike is that now I “feel” the update process due to a response lag when using the rest of my gui.
Thanks for helping so far. :smiley:

The lag was caused by calling:

gSystem -> GetCpuInfo(&cpuInfo, 400);

I know update the graphics with the use of a timer an gather the data within a own thread.
The Frame now behave as I expected. :smiley:

Thanks for pointing out that the GUI isn’t really thread save, I would have searched on and on without a clue where I have to look… =D>


Hi Johannes,

You’re welcome! Glad to see you solved the problem! :smiley:

Cheers, Bertrand.