g_timeout_add (100, (GSourceFunc) read_next_packets, NULL);
由于上述陈述,我觉得GUI响应很慢。
如何让它以异步方式工作,以免影响GUI响应?
答案 0 :(得分:3)
在主线程中调用这些函数的回调。如果read_next_packets
缓慢或阻塞I / O,则应为其创建一个不会干扰GUI的单独线程。当该线程需要通知主线程时,然后它可以使用g_idle_add
将执行转移到主线程范围。
在伪代码中:
// In a dedicated thread:
while (...) {
Package* package = do_read (); // This call is slow or blocks.
if (package)
g_idle_add ((GSourceFunc) process_package, package);
}
// This is called in the main thread. Should be fast to not freeze GUI.
gboolean
process_package (Package* package)
{
...
package_free (package);
}