How to update MyGUI widgets from another thread

caohuuloc

08-05-2009 09:44:30

I am trying to use another thread to update MyGUI widgets. But this sometimes causes the program crash. Anybody know how to solve this problem? Can I let MyGUI stop rendering widgets when I update gui from another thread, then let MyGUI start rendering widgets again?
Please help!!!

caohuuloc

11-05-2009 05:11:43

Here is the example code:

void ThreadList(void *param)
{
MyGUI::ListPtr lst = (MyGUI::ListPtr)param;
bool add = true;
while (1)
{
Sleep(1000);
if (add)
{
char buff[100];
for (int i = 1; i <= 50; i++)
{
sprintf(buff, "Item %d", i);
lst->addItem(buff);
//Sleep(50); //Uncomment this line, it works better but somtimes still crashes
}
}
else
{
lst->removeAllItems();
}
add = !add;
}
}

void DemoKeeper::createScene()
{
MyGUI::ListPtr lst = mGUI->createWidget<MyGUI::List>("List", 50, 50, 300, 500, MyGUI::Align::Default, "Main");
HANDLE h = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadList, lst, 0, NULL);
}

Five_stars

11-05-2009 16:41:44

MyGUI isn't thread safe. You have to set locks at start and end of frame and in all your functions, where you call MyGUI :).

caohuuloc

12-05-2009 04:11:09

I use the framework of demo MyGUI:

class demo::DemoKeeper inherits base::BaseManager
I override method DemoKeeper::createScene() and create thread in this method. You can see the code in my post.

You said that: "You have to set locks at start and end of frame and in all your functions". Can you tell me what exactly I should do?

Five_stars

13-05-2009 04:58:47

Under "lock" I mean set mutexes or crititical sections. I haven't use threads in C++, but I think you can look into defines definion of OGRE_*_MUTEX at http://www.ogre3d.org/docs/api/html/Ogr ... es_8h.html

caohuuloc

13-05-2009 05:26:14

Dear Five_stars,

I know what you want to say. I should set critical section at place where frame is rendered. But I don't know exactly where I should set critical section. Because I am a newbie in using Ogre. Can you tell me where I should set critical section?

I also have some ideas to solve this problem but I don't know if it is possible with Ogre:
- Sol #1: Stop rendering frames, update data of GUI, start rendering frames.
- Sol #2: Invoke MyGUI to update GUI later (but we don't update GUI directly)

Anybody know about this, please help me!

my.name

16-05-2009 14:06:58

use mutex

caohuuloc

16-05-2009 18:59:30

Where to enter and leave mutex?