问题
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?
回答1:
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.
Furthermore, to use networking APIs, you don't need to be stuck in the main thread - you can handle them in a separate thread.
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 :)
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.
来源:https://stackoverflow.com/questions/50699559/how-can-i-add-a-progress-for-message-sending-operation-smtp-with-blocking-sock