【问题标题】:Possible to reuse my GCancellable instance?可以重复使用我的 GCancellable 实例吗?
【发布时间】:2018-01-20 09:46:17
【问题描述】:

在通过 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,考虑到有待处理的调用?这一定是一个标准的用例..不知道有没有更好的解决方案。

【问题讨论】:

  • 您知道为什么要为objective-c 添加标签吗?它是另一种语言,所以它似乎与我无关。
  • 先取消,然后重置,然后再使用呢?
  • 就像你说的,重置正在使用的可取消是未定义的,但是如果你先取消它,那么它就没有被使用,你应该能够重置它以供重复使用。
  • 感谢何塞丰特!现在似乎可以正常工作了..我只是不知道取消时可取消的不再“正在使用”。
  • 请随意用您自己的回答“关闭它”来完成您的问题,并作为未来读者的参考。总帐

标签: c gtk3 glib gdbus


【解决方案1】:

我的第一次编码尝试几乎没问题..我只是没有意识到可取消的不再“正在使用”,当它被取消时,所以在调用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);

【讨论】:

    猜你喜欢
    • 2012-04-09
    • 1970-01-01
    • 2021-08-27
    • 2015-02-24
    • 2011-05-14
    • 2015-04-07
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    相关资源
    最近更新 更多