Difference between smtpClient.send() and smtpClient.SendAsync()? - smtpclient

I am trying to send mail from localhost..
and on doing this i have got methods from different sites to sending mails..but on doing this i am confused between smtpClient.send() and smtpClient.SendAsync()..
I want to know that How they are different from each other???
Thanks in advance..

smtpClient.send() will initiate the sending on the main/ui thread and would block. smtpClient.SendAsync() will pick a thread from the .NET Thread Pool and execute the method on that thread. So your main UI will not hang or block.
Async Method Invocation - http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx

SendAsyc - Sends the specified e-mail message to an SMTP server for delivery. This method does not block the calling thread and allows the caller to pass an object to the method that is invoked when the operation completes. More details : SmtpClient.SendAsync Method

Related

Remove particular class's completion handler from array

I have implemented one method of completion handler which fetch data from server.
While one request is in progress, this method may be called multiple time by any other class and I want to notify all once data is received from server without sending multiple request.
To achieve this I am adding up completion handlers in thread safe array.
But I want to remove particular completion handler if caller class doesn't exists in memory when data is received from server.
Ex - If controller's viewDidDisAppear() gets called. I want to removed its handler.
How to achieve this?

Wait for async calls to return before exiting program when using AWS-cpp-sdk

I am doing a POC on using the AWS-cpp-sdk
For which I wrote a simple program to send messages to the SQS queue.
I am using the SendMessageAsync method to send the messages like below.
sqsClient->SendMessageAsync(sendMessageRequest, &sendMessageCallBack);
My program crashes since my program is exiting before the async method returns and Aws::ShutdownAPI(options); terminates the threads created by the Async method call.
I found that the AWS-sdk for JAVA suggests the following for exactly this scenario.
`
/**
* Shuts down the client, releasing all managed resources. This includes
* forcibly terminating all pending asynchronous service calls. Clients who
* wish to give pending asynchronous service calls time to complete should
* call getExecutorService().shutdown() prior to calling this method.
*/
#Override
public void shutdown() {
super.shutdown();
executorService.shutdownNow();
}`
I am unable to find something equivalent in the AWS cpp SDK.
Can someone suggest what would be the best way to fix this issue.
You are responsible for making sure your requests are done before calling ShutdownAPI(). This is usually only an issue in contrived "sample app" sorts of scenarios where you are performing the operation directly inside your main() function. You also need to make sure the SQS client is deleted before ShutdownAPI is called.
On option is to use a std::condition_variable(semaphore) to synchronize before the exit. You can pass the semaphore to your callback and notify_one() at the end of the callback. Then, before shutdown you can call wait() on the semaphore.

DBUS - multithread processing

I have a main loop in my program, which calls this method from dbus:
dbus_connection_read_write_dispatch
I have some registered callbacks, which are invoked, when message arrives. Within this callback I am also processing the response and sending back response. Problem is that sometimes it takes much time so probably it will block receiving messages from DBUS.
Question - can I call dbus_connection_read_write_dispatch() method on the same connection from more than one thread? Then it will be probably possible to receive new DBUS messages while the previous one is being processed.
Or maybe better idea is to process responses in another thread than the main loop, from callback is invoked?
Thank you
you can call dbus_connection_read_write_dispatch() from multiple threads if you have called the function dbus_threads_init_default() atleast once.Instead a better approach is to have a single thread running dbus dispatcher and use a thread-pool to process the data from callbacks.
See dbus_threads_init_default() for more info.
By the document provided by freedesktop.org, you can.
But if you operate with same DBusConnection instance from different threads directly, eg. calling dbus_connection_send_with_reply_and_block in a thread while anothoer thread is blocking on dbus_connection_read_write_dispatch, the connection maybe work unproperly. According to official document, DBus connection will be locked when calling callback functions.DBusConnection
In my situation, the dbus_connection_send_with_reply_and_block didn't return even if the return message was send to my process (I had seen it on dbus-monitor). Calling dbus_thread_init does not work at all.
Recently I used a delegate to send / receive / dispatch all dbus messages in one thread, and problem disappeared.
A mail in mailing list of freedesktop.org

How to stop QNetworkAccessManager from getting a reply C++

Hi I have a QNetworkAccessManager which I use to send request to get image data from server. This call is asynchronous. I do multiple calls with it. Each call is done by opening a new instance of QNetworkAccessManager So when a specific condition occurs I want to stop the QNetworkAccessManager from receiving the replies from its network requests. Is there any way to do it? Thanks.
Don't use a new QNetworkAccessManager for each request but share the manager. It's usually fine to have just one. Of course one can have multiple if the application design suggests it - but e.g. managing multiple of them in a single controlling object is usually unnecessary. Just have one manager with the same lifetime as the object controlling the network requests.
To cancel running operations, keep the QNetworkReply* pointers QNetworkAccessManager::get/put/post return and call abort() when your condition occurs.
Connect to the finished() signal to remove them from the bookkeeping (as otherwise you would end up with dangling pointers).
If that becomes too complicated, think about using the command pattern. In this answer I describe why I find it particularly useful in this context.

Boost ASIO Network Timing Issue

I am using boost::asio to implement network programming and running into timing issues. The issue is currently most with the client.
The protocol initially begins by the server returning a date time string to the user, and the client reads it. Up to that part it works fine. But What I also want is to be able to write commands to the server which then processes them. To accomplish this I use the io_service.post() function as shown below.
io_service.post(boost::bind()); // bounded function calls async_write() method.
For some reason the write tries happens before the initial client/server communication, when the socket has not been created yet. And I get bad socket descriptor error.
Now the io_service's run method is indeed called in another thread.
When I place a sleep(2) command before post method, it work fine.
Is there way to synchronize this, so that the socket is created before any posted calls are executed.
When creating the socket and establishing the connection using boost::asio, you can define a method to be called when these operations have either completed or failed. So, you should trigger your "posted call" in the success callback.
Relevant methods and classes are :
boost::asio::ip::tcp::resolver::async_resolve(...)
boost::asio::ip::tcp::socket::async_connect(...)
I think the links below
will give u some help
http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/io_service.html