ProcessEvents and keyboard input

Hi,

this code crashes when I make keyboard input on the console window.
I run this on windows (.Net 2003) and ROOT 5.08/00.

#include “TRint.h”
#include “TSystem.h”

int main(int argc, char *argv[])
{
TRint *app = new TRint(“App”, &argc, argv,NULL,0,true);

while(1) {
gSystem->ProcessEvents();
gSystem->Sleep(10);
}
return 0;
}

Matthias

Hi Matthias,

I don’t understand what you are trying to do…
If you create an instance of TRint, just call app->Run();
gSystem->ProcessEvents() handles GUI, timers, and sockets events.
Terminal (keyboards) events are processed in TApplication::Run().

Cheers,
Bertrand.

Hi Bertrand,

I can’t use app->Run() because I don’t want to be stuck in a ROOT session.

I like to display a GUI that can handle user input while I run some code.

So my program will look some thing like this :

StartWindow();
while (!terminationFlag) {
handleEvents();
}

If I run this code I need somewhere in the loop a call to gSystem->ProcessEvents() or something equivalant (if there is?). Otherwise I can’t use the GUI.

Everything works fine, only that the program crashes when I make a keyboard input on the console window.

Of course I could avoid making queries in the console window but I don’t like my program to crash when the user presses a key in the wrong window.

Matthias

Hi Matthias,

Something like this :

[code]#include “TCanvas.h”
#include “TPaveLabel.h”
#include “TApplication.h”
#include “TSystem.h”

int main(int argc, char *argv[])
{
TApplication *app = new TApplication(“App”, &argc, argv);

TCanvas *c = new TCanvas(“c”, “The Hello Canvas”, 400, 400);
c->Connect(“Closed()”, “TApplication”, &app, “Terminate()”);

TPaveLabel *hello = new TPaveLabel(0.2,0.4,0.8,0.6,“Hello World”);
hello->Draw();
TPaveLabel *quit = new TPaveLabel(0.2,0.2,0.8,0.3,“Close via menu File/Quit”);
quit->Draw();
c->Update();

while(1) {
gSystem->ProcessEvents();
gSystem->Sleep(10);
}
return 0;
}
[/code]
Using TRint means that you want an interactive ROOT session, so in your case, it is better to use TApplication.
And if you don’t need the console window, you can specify :
/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup
in your makefile, or in your project, then the console will not show up.
HTH.
Cheers, Bertrand.

Hi Bertrand,

thanks for that.

The problem is that I want to start interactive root sessions in the loop at certain points. So I need the TRint.

Is it not possible to make your example run with TApplication replaced by TRint?

Matthias

Hi Matthias,

The problem is that TRint input processing is done in Run()…
The solution would be to create a multithreaded application, but with synchronisation mechanism :wink:
(please take a look at threads samples in test and tutorials)

Cheers,
Bertrand.

Hi Bertrand,

I solved the problem by deriving my own Rint class :

This modification could maybe be done directly in the TRint class.

class MyRint : public TRint {
private:
Bool_t fRunning;

public:

MyRint(const char *appClassName, int *argc, char **argv,
void *options = 0, int numOptions = 0, Bool_t noLogo = kFALSE)
: TRint(appClassName, argc, argv, options, numOptions,noLogo)
{
fRunning = false;
};

Bool_t HandleTermInput()
{
if (fRunning)
return TRint::HandleTermInput();
return true;
}

void Run(Bool_t retrn)
{
fRunning = true;
TRint::Run(retrn);
fRunning = false;
}
};

Matthias