如何使用阻止套接字API添加消息发送操作(smtp)的进度? [英] How can I add a progress for message sending operation (smtp) with blocking sockets API's?
问题描述
我正在使用阻塞套接字API(waitFor *函数)通过smtp协议(这是DLL模式)发送邮件.所有操作都是同步的:connect->waitForConnected(timeout)->login->waitForReadyRead(timeout)->sendMessage->waitForBytesWritten(timeout)
-> etc.
我正在使用阻止API,因为需要QCoreApplication
缺少(不同应用程序使用的DLL,包括基于非qt的应用程序).阻塞函数不需要事件循环,并且可以正常工作.
但是,如何对长期发送邮件操作(例如带有大附件)进行可视化处理?
以及如何在sendmail
DLL中组织用于进度通知的回调?
ps:所有阻塞的waitFor *函数都标记为
功能,在Windows上可能会随机失败.如果您的软件将在Windows上运行,请考虑使用事件循环和readyRead()
信号.
为什么?
I'm using blocking sockets API (waitFor* functions) for sending mail by smtp protocol (it's a DLL modue). All operations are synchronous: connect->waitForConnected(timeout)->login->waitForReadyRead(timeout)->sendMessage->waitForBytesWritten(timeout)
->etc.
I'm using blocking API, because QCoreApplication
absence is required (DLL used by different apps, incl. non-qt-based). Blocking functions don't require event loop and it works fine.
But how can I make a visual progress for long-term sending mail operations (with big attachment, for example)?
And how can organize callbacks for progress notifications in sendmail
DLL?
ps: all blocking waitFor* functions marked as
functions, that may fail randomly on Windows. Consider using the event loop and the readyRead()
signal if your software will run on Windows.
Why?
推荐答案
在使用DLL时,您绝对可以拥有QApplication
实例(因为您要使用基于窗口小部件的gui,因此必须为QApplication
而不是QCoreApplication
).它集成到主线程的本机消息循环中.请记住,要运行Qt Gui,您只需要一个本机事件循环-不必将代码停留在QCoreApplication::exec
中.您需要该应用程序的实例,并且需要通过调用一次exec
并确保它返回(即通过使用零超时计时器)来启动它,但是仅此而已.除此之外,应用程序的主线程的消息泵将为您处理事情.
You can definitely have a QApplication
instance when using DLLs (it must be QApplication
not QCoreApplication
since you want a widget-based gui). It integrates into the native message loop of the main thread. Remember that for Qt Gui to run you only need a native event loop - the code doesn't have to be stuck within QCoreApplication::exec
. You need an instance of the application, and you need to prime it by invoking exec
once, and ensuring that it returns (i.e. by using a zero-timeout timer), but that's all. Past that, the application's main thread's message pump will handle things for you.
此外,要使用网络API,您无需卡在主线程中-您可以在单独的线程中处理它们.
Furthermore, to use networking APIs, you don't need to be stuck in the main thread - you can handle them in a separate thread.
您的DLL与不会在主线程中运行消息泵的控制台应用程序不兼容,但是您可以作弊:仅在Windows上,QCoreApplication
及其派生类可以是在任何线程中使用:)
Your DLL won't be compatible with console applications that don't run a message pump in the main thread, but then you can cheat: on Windows, and on Windows only, the QCoreApplication
and its derived classes can be used in any thread :)
您绝对必须将DLL与Qt静态链接,或使用放置在唯一名称空间中的动态链接Qt.请记住,如果您链接的应用程序使用Qt,则绝对不能保证它们构建的Qt与您使用的Qt二进制兼容.即使是相同的版本.
You definitely must either statically link your DLL with Qt, or use a dynamically-linked Qt that was put in a unique namespace. Remember that if the application you link with uses Qt, there are absolutely no guarantees that the Qt they built is binary-compatible with the Qt that you use. Even if it's the same version.
这篇关于如何使用阻止套接字API添加消息发送操作(smtp)的进度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!