可以重用我的 GCancellable 实例吗?

Possible to reuse my GCancellable instance?

在通过 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,因为文档统计如下:

If cancellable is currently in use by any cancellable operation then the behavior of this function is undefined.

是否可以检查我的 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,认为有待处理的呼叫?这一定是一个标准用例..不知道有没有更好的解决方案。

我的第一次编码尝试几乎没问题。我只是没有意识到可取消的不再是 "in use",当它被取消时,所以在调用 g_cancellable_cancel it is safe to call 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);