【发布时间】:2011-07-31 10:23:44
【问题描述】:
我创建了一个具有异步OpenWebPage() 函数的类。一旦你调用OpenWebPage(someUrl),就会调用一个处理程序——OnPageLoad(reply)。我一直在使用一个名为 lastAction 的全局变量来处理页面加载后的内容 - 处理程序检查 lastAction 是什么并调用适当的函数。例如:
this->lastAction == "homepage";
this->OpenWebPage("http://www.hardwarebase.net");
void OnPageLoad(reply)
{
if(this->lastAction == "homepage")
{
this->lastAction = "login";
this->Login(); // POSTs a form and OnPageLoad gets called again
}
else if(this->lastAction == "login")
{
this->PostLogin(); // Checks did we log in properly, sets lastAction as new topic and goes to new topic URL
}
else if(this->lastAction == "new topic")
{
this->WriteTopic(); // Does some more stuff ... you get the point
}
}
现在,当我们有大量“动作”时,很难编写和跟踪。当我在 Python 中(同步)做一些事情时,它要容易得多,比如:
OpenWebPage("http://hardwarebase.net") // Stores the loaded page HTML in self.page
OpenWebpage("http://hardwarebase.net/login", {"user": username, "pw": password}) // POSTs a form
if(self.page == ...): // now do some more checks etc.
// do something more
现在想象一下,我有一个包含以下操作的队列类:主页、登录、新主题。我应该如何通过异步回调执行所有这些操作(以正确的顺序,一个接一个!)?第一个例子显然是完全硬编码的。
我希望你能理解我的问题,因为坦率地说,我担心这是有史以来最糟糕的问题:x
附:所有这些都在 Qt 中完成。
【问题讨论】:
标签: c++ qt asynchronous