Threads with standalone program

Hello Rooters

I am trying to build a standalone C++ program (that is, that contains a “main”) wich compute some quantities and use ROOT graphics to display results as the computation goes.

Unfortunately, I cannot interact with ROOT Canvas while the main program is running. I have attached a small example to show the problem.

There is an example in ROOT ( root/test/threads.cxx ) using TThreads to solve this problem but I was wondering if there was not an easier way.

main.cxx (2.19 KB)

Hi Matthieu,

Here is the easy way: :wink:
Just add gSystem->ProcessEvents() in your “while (!stop)” loop, or in the surfaces() function, i.e. after c1->Update();


Hello Bertrand

I’ve tried what you said but it doesn’t work. Using “gsystem->ProcessEvents() ;” I have now, on the TCanvas, the menu bar but I still cannot interact with the Graph in the TCanvas (to rotate the graph for instance), neither can I close the TCanvas.

I have written a simpler example to show the problem.
I am using ROOT 5.17/04 compiled with gcc4.2.2 on a Debian Linux box and to compile this code in a file named “test.cxx” I am using the following command:

g++ root-config --libs -Iroot-config --incdir test.cxx -o test

#include <cstdlib>
#include <ctime>
#include "TSystem.h"
#include "TApplication.h"
#include "TCanvas.h"
#include "TF2.h"

int main( int argc , char* argv[] )
	TApplication theApp("App", &argc, argv);
	TCanvas  c1("c1","Test",1);
	TF2 f2("f2","x**2 + y**2 - x**3 -8*x*y**4",-1,1.2,-1.5,1.5);

	c1.Update() ;
	gSystem->ProcessEvents() ;
	// loop for waiting 10 seconds 
	time_t t0 = time(0);
	unsigned int wait = 10 ; // 10 seconds to wait	 
	while( static_cast< unsigned int >(time(0) - t0) < wait);

	theApp.Run() ;	

The “while” loop is just here to simulate some computation. I can interact with the Canvas only after the 10 seconds, not during the while loop.

Any idea what is wrong?


Hi Matthieu,

:open_mouth: You must put gSystem->ProcessEvents() INSIDE your loop!
the goal of gSystem->ProcessEvents() is to allow you to interact with the GUI/graphics, then it MUST BE inside the loop.


Oups! Thanks Bertrand.

It works

You are welcome :slight_smile:

Cheers, Bertrand.