3

在通过 gdbus 触发代理调用之前,我想取消此 dbus 方法上任何可能的挂起调用。我的第一次尝试是这样的:

// in the "member" list of my widget
GCancellable   *my_cancellable;

// in the init method of my widget:
plugin->my_cancellable = g_cancellable_new ();

// in the method which does the call
g_cancellable_cancel (plugin->my_cancellable);
my_gdbus_call_something (plugin->proxy, plugin->my_cancellable, reply_handler,plugin);

那没有成功,因为使用相同的可取消实例也会取消任何未来的调用。看起来我不能使用g_cancellable_reset,因为文档统计如下:

如果可取消操作当前正在被任何可取消操作使用,则此函数的行为未定义。

是否可以检查我的 GCancellable 的使用状态?它会帮助我吗?

对我来说已经很好的是为每个呼叫创建一个新的可取消:

// in the "member" list of my widget
GCancellable   *my_cancellable;

// in the init method of my widget:
plugin->my_cancellable = NULL;

// in the method which does the call
if(plugin->my_cancellable != NULL)
  {
    g_cancellable_cancel (plugin->my_cancellable);
    g_object_unref (plugin->my_cancellable);
  }
plugin->my_cancellable = g_cancellable_new ();
my_gdbus_call_something (plugin->proxy, plugin->my_cancellable, reply_handler,plugin);

是否保存到 unref my_cancellable,考虑到有待处理的呼叫?这一定是一个标准的用例..我想知道是否没有更好的解决方案。

4

1 回答 1

0

我的第一次编码尝试几乎没问题..我只是没有意识到可取消的不再“正在使用”,当它被取消时,所以在调用 g_cancellable_cancel 之后调用g_cancellable_reset安全的

感谢 José Fonte 指出这一点!

这里是固定代码:

// in the "member" list of my widget
GCancellable   *my_cancellable;

// in the init method of my widget:
plugin->my_cancellable = g_cancellable_new ();

// in the method which does the call
g_cancellable_cancel (plugin->my_cancellable);
g_cancellable_reset (plugin->my_cancellable);
my_gdbus_call_something (plugin->proxy, plugin->my_cancellable, reply_handler,plugin);
于 2018-02-04T18:26:17.710 回答