Display Dialog/TGMsgBox from a TThread


Please help. I’m having an “Access Violation” crash when I launch a thread and attempt to create a TGMsgBox from a thread routine.

We have an existing C/C++ ROOT application using ROOT version 28.00b. The application is built using VS2008 and runs on Win 7, 32 bit.

I’ve created a test thread function as such:
void *PopUp(void *p)
MyMainFrame *mthis = (MyMainFrame *)p;
int retcode;

TGFrame *pCF = new TGFrame(gClient->GetRoot(),TVirtualX::GetWindowID(wid), (TGWindow *)p);

TGMsgBox  mymsgBox = new TGMsgBox(pCF, 
						"Running Thread!", 
						"Thread is Running and waiting to terminate.",
						kMBIconAsterisk, kMBOk, &retcode);

return 0;


In a class method:
class MyMainFrame: public TGMainFrame
MyMainFrame:TestThread() we launch a thread as follows:

TThread *myThread = new TThread("myThread", PopUp, this);



The moment I hit the TGMsgBox() line, I get an access violation. What am I doing wrong?


You can only create GUI elements from the main thread…

EDIT: BTW, you should call TGMsgBox *mymsgBox = new TGMsgBox(...) instead of TGMsgBox mymsgBox = new TGMsgBox(...)
Or even simpler, just call:

new TGMsgBox(gClient->GetDefaultRoot(), (TGMainFrame *)p, "Running Thread!", "Thread is Running and waiting to terminate.", kMBIconAsterisk, kMBOk, &retcode);
Cheers, Bertrand.

P.S. Here is a simple example, running on Windows with ROOT v5-34-00-patches. guiThread.tar.gz (1.4 KB) The instructions to build it are in the guiThread.cxx file

Cheers, Bertrand.

Hi Bellenot,

Thank you for your help. We were unaware that we could not create a GUI element inside a thread.

The example files you sent also helped greatly.

The reason we wanted a separate thread to pop up a gui was to have a modaless dialog pop up and update the dialog with a time. What would be the best way to create a modal-less dialog in the main GUI thread? I can easily create a thread to send a message/timer to the dialog and update the time/clock display on the modal-less gui. Just don’t know the best way to create the Modal-less dialog.

Thank you,

Hi Angel,

There are several ways of achieving what you’re asking for. If the example I sent is not enough, I can prepare another example

Cheers, Bertrand.


The example is enlightening, but it will be different that what we need to implement. Here is what we need to do.

We have the main GUI with a bunch of windows controls (Buttons, check boxes, endit, list boxes etc…).

In the main GUI, there will be a button we press to perform a particular multistep function. Several dialog boxes with OK/Cancel will be displayed. Each Dialog will have a text area where there is a count down timer displaying the remaining time for the step of the process were are in. Once the timer reaches 0, the user can click on OK to pop-up the next dialog with a countdown timer. This goes on for about 8 dialogs.

We did not want the dialogs popping up interfering with the main GUI thread which is why we through of poping up a GUI within a thread (now we know we can’t do it.).

If you have a suggestion, please do suggest…


Hi Angel,

Then simply create your own non-blocking version of Message Dialog… (i.e. don’t call fClient->WaitFor(this):wink:

Cheers, Bertrand

Hi Bellenot,

Sorry, my I’m a new person to Root and not clear on what you mean by create your own Message Box.

I looked at TGMsgBox class, to see how I can create my own implementation. It’s straight forward to implement, but I just don’t see how I can update the const char *title or msg in real-time every second once MyTGMsgBox has been created. See below.

const TGWindow * p = 0,
const TGWindow * main = 0,
const char * title = 0,
const char * msg = 0,
const TGPicture * icon = 0,
Int_t buttons = kMBDismiss,
Int_t * ret_code = 0,
UInt_t options = kVerticalFrame,
Int_t text_align = kTextCenterX | kTextCenterY

However, perhaps if I go higher in the inheritance tree to TGMainFrame or higher and create a dialog/frame, add buttons, text area and then upate the text area as time elapses. Is this what you are talking about?

Using TGMainFrame or higher to create a “Message Box”?



Here is another example (showing also the ctrl-z key handling). You should play with it and try to understand it (then you can simplify the code and adapt it to your need), I’ll be on vacation until beginning of January… :wink:
guiThread2.tar.gz (4.31 KB)
Cheers, Bertrand.

Thank you.

You have a wonderful vacation.