# Threads with standalone program

**URL:** <https://root-forum.cern.ch/t/threads-with-standalone-program/5642>\
**Category:** ROOT\
**Created:** [November 16, 2007, 1:44pm UTC](https://root-forum.cern.ch/t/threads-with-standalone-program/5642 "2007-11-16T13:44:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthieuguillo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@matthieuguillo](https://root-forum.cern.ch/u/matthieuguillo)\
**Post date:** [November 16, 2007, 1:44pm UTC](https://root-forum.cern.ch/t/threads-with-standalone-program/5642/1 "2007-11-16T13:44:18Z")

</div>

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.

Thanks  
[main.cxx](https://root-forum.cern.ch/uploads/default/original/2X/c/cc561d13f1c0d80057b5245c7026bd908e415b17.cxx) (2.19 KB)

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [November 16, 2007, 4:51pm UTC](https://root-forum.cern.ch/t/threads-with-standalone-program/5642/2 "2007-11-16T16:51:52Z")

</div>

Hi Matthieu,

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

Bertrand.

---

<div class="post-metadata">

**Author:** ![matthieuguillo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@matthieuguillo](https://root-forum.cern.ch/u/matthieuguillo)\
**Post date:** [November 19, 2007, 11:04am UTC](https://root-forum.cern.ch/t/threads-with-standalone-program/5642/3 "2007-11-19T11:04:55Z")

</div>

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` -I`root-config --incdir` test.cxx -o test

```auto
#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);

	f2.Draw("surf4");
	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() ;	
	
	return EXIT_SUCCESS;
}
```

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?

Thanks

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [November 19, 2007, 11:10am UTC](https://root-forum.cern.ch/t/threads-with-standalone-program/5642/4 "2007-11-19T11:10:30Z")

</div>

Hi Matthieu,

😮 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.

Bertrand.

---

<div class="post-metadata">

**Author:** ![matthieuguillo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@matthieuguillo](https://root-forum.cern.ch/u/matthieuguillo)\
**Post date:** [November 19, 2007, 11:25am UTC](https://root-forum.cern.ch/t/threads-with-standalone-program/5642/5 "2007-11-19T11:25:43Z")

</div>

Oups! Thanks Bertrand.

It works

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [November 19, 2007, 11:27am UTC](https://root-forum.cern.ch/t/threads-with-standalone-program/5642/6 "2007-11-19T11:27:14Z")

</div>

You are welcome 🙂

Cheers, Bertrand.
