Search The ForumSearch   RegisterRegister  LoginLogin

MailBee.NET IMAP

 AfterLogic Forum : MailBee.NET IMAP
Subject Topic: Handling exceptions in async operations? Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
Blixt
Newbie
Newbie


Joined: 24 August 2009
Location: Sweden
Online Status: Offline
Posts: 10
Posted: 26 August 2009 at 6:27am | IP Logged Quote Blixt

Hi there,

I've got an Imap object idling for a longer
period of time using the BeginIdle method.
Sometimes, the connection is interrupted,
causing a
MailBee.MailBeeAbortedByRemoteHostException
exception. While I can see the error before it
occurs using the ErrorOccurred event, I cannot
handle the exception (i.e., reconnect.) Instead,
the application crashes. The code that throws
the exception is in a thread created by the
MailBee.NET code so I can't catch it.

Should I create my own thread that does idling
using Idle, or is there a way to handle the
exception?
Back to Top View Blixt's Profile Search for other posts by Blixt
 
Igor
AfterLogic Support
AfterLogic Support


Joined: 24 June 2008
Location: United States
Online Status: Offline
Posts: 6104
Posted: 26 August 2009 at 6:39am | IP Logged Quote Igor

The main reason of the issue is that IMAP server will stop idling and will close connection if idling is active for too long time (29 mins is default limit). Thus, we suggest to add a timer-based handler which performs reconnection to IMAP server, say, every 15-20 mins.

Regarding handling exceptions of asynchronous methods, you'll find an example on this documentation page; the approach assumes using callback function with a try/catch block.

--
Regards,
Igor, AfterLogic Support
Back to Top View Igor's Profile Search for other posts by Igor
 
Blixt
Newbie
Newbie


Joined: 24 August 2009
Location: Sweden
Online Status: Offline
Posts: 10
Posted: 26 August 2009 at 6:43am | IP Logged Quote Blixt

Yeah, in this case it was the timeout (after
exactly 35 minutes), but I want to make my code
safe even if the network connection goes down or
something like that. I will implement a
reconnection every 20 minutes or so.

Okay, so is it safe to assume that all
exceptions are thrown in the EndIdle method? So
no exceptions are thrown in the thread spawned
by BeginIdle?
Back to Top View Blixt's Profile Search for other posts by Blixt
 
Igor
AfterLogic Support
AfterLogic Support


Joined: 24 June 2008
Location: United States
Online Status: Offline
Posts: 6104
Posted: 26 August 2009 at 6:48am | IP Logged Quote Igor

Yes, quite right. Note that BeginIdle may still cause exceptions connected to errors in the code, e.g. wrong method parameters or method call when there's no active connection to server.

--
Regards,
Igor, AfterLogic Support
Back to Top View Igor's Profile Search for other posts by Igor
 
Blixt
Newbie
Newbie


Joined: 24 August 2009
Location: Sweden
Online Status: Offline
Posts: 10
Posted: 26 August 2009 at 6:58am | IP Logged Quote Blixt

Okay! Thanks once again for your quick help! =)
Back to Top View Blixt's Profile Search for other posts by Blixt
 
Blixt
Newbie
Newbie


Joined: 24 August 2009
Location: Sweden
Online Status: Offline
Posts: 10
Posted: 02 September 2009 at 4:48am | IP Logged Quote Blixt

Hi again,

This code has been running fine for a few days
since I added periodical reconnection. I also
added try...catch in the EndIdle to handle
errors. However, a connection error occurred
recently that appears to be outside my code:

Code:

MailBee.MailBeeSocketObjectDisposedException:
The socket object used by MailBee was
unexpectedly closed. InnerException message
follows: ---> System.IO.IOException  --->
System.Net.Sockets.SocketException
   
System.Net.Sockets.NetworkStream.Read(Byte[]
buffer, Int32 offset, Int32 size)

   
System.Net.Sockets.NetworkStream.Read(Byte[]
buffer, Int32 offset, Int32 size)
   dr.a(Byte[] A_0, Int32 A_1)

   dr.a(Exception A_0)
   dr.a(Byte[] A_0, Int32 A_1)
   c0.ak()
   gx.u()
   cx.e(Boolean A_0)
Back to Top View Blixt's Profile Search for other posts by Blixt
 
Igor
AfterLogic Support
AfterLogic Support


Joined: 24 June 2008
Location: United States
Online Status: Offline
Posts: 6104
Posted: 02 September 2009 at 5:06am | IP Logged Quote Igor

To let us help you on this, please enable logging IMAP session into a file, reproduce the issue and provide us with the log file for examination.

--
Regards,
Igor, AfterLogic Support
Back to Top View Igor's Profile Search for other posts by Igor
 
Blixt
Newbie
Newbie


Joined: 24 August 2009
Location: Sweden
Online Status: Offline
Posts: 10
Posted: 06 September 2009 at 11:18pm | IP Logged Quote Blixt

Hello again. I enabled logging and here are the
last few lines before the error:
Code:
[10:48:55.39] [INFO] Will go idle.
[10:48:55.39] [SEND] MBN00000006 IDLE\r\n
[10:48:55.39] [RECV] + IDLE accepted, awaiting
DONE command.\r\n [Total 41 bytes received.]
[11:08:55.32] [INFO] Will finish idling.
[11:08:55.32] [SEND] DONE\r\n
[11:09:14.28] [INFO] Error: The socket object
used by MailBee was unexpectedly closed.
InnerException message follows: Det gick inte
att läsa data från transportanslutningen:
Anslutningsförsöket misslyckades eftersom den
anslutna datorn inte svarade inom en viss tid
eller på grund av att den etablerade
anslutningen till värddatorn inte längre
fungerar.
[11:09:14.32] [INFO] Will disconnect from host
"172.31.152.80".
[11:09:14.32] [INFO] Disconnected from host
"172.31.152.80".


The server is running in Swedish so the
exception is localized, but I'll try to
translate it: "Could not read data from the
transport connection: The connection attempt
failed due to the connected computer not
responding within a certain period of time or
because the established connection to the host
is no longer working."
Back to Top View Blixt's Profile Search for other posts by Blixt
 
Alex
AfterLogic Support
AfterLogic Support
Avatar

Joined: 19 November 2003
Online Status: Offline
Posts: 2206
Posted: 07 September 2009 at 5:20am | IP Logged Quote Alex

The world is unfair. Although IMAP RFC claims the server must support 30 minutes of idling, many servers ignore this and close the connection after just a few minutes. Your case, it seems. Try to lower the reconnection interval to 3 minutes.

Regards,
Alex
Back to Top View Alex's Profile Search for other posts by Alex
 
Blixt
Newbie
Newbie


Joined: 24 August 2009
Location: Sweden
Online Status: Offline
Posts: 10
Posted: 07 September 2009 at 5:44am | IP Logged Quote Blixt

Hello Alex,

The service usually works fine for a few days,
then a hiccup occurs which throws an exception
that I cannot catch. So it doesn't matter if I
reconnect every minute, the connection can
still break unexpectedly and in such a case I
have to handle the exception.

If this isn't possible I'll have to put the
connection handler on its own thread and
forcefully restart it as well to prevent the
main thread from crashing. But it would be
more convenient to be able to catch the
exception, since it's obviously caught by your
library before being passed on (but not to my
code, it seems?)
Back to Top View Blixt's Profile Search for other posts by Blixt
 
Alex
AfterLogic Support
AfterLogic Support
Avatar

Joined: 19 November 2003
Online Status: Offline
Posts: 2206
Posted: 07 September 2009 at 6:10am | IP Logged Quote Alex

This topic discusses handling exceptions in worker threads: Not receiving idle notifications

Regards,
Alex
Back to Top View Alex's Profile Search for other posts by Alex
 
setven17
Newbie
Newbie


Joined: 04 October 2009
Location: Philippines
Online Status: Offline
Posts: 2
Posted: 05 October 2009 at 1:41am | IP Logged Quote setven17

whoa! at last I've come up on the solution. thanks even confusion took place before on the codes but it's okay now.
Back to Top View setven17's Profile Search for other posts by setven17
 
tromby24
Newbie
Newbie


Joined: 23 October 2009
Location: Philippines
Online Status: Offline
Posts: 2
Posted: 24 October 2009 at 2:12am | IP Logged Quote tromby24

     
Tengo un servidor IMAP objeto ralentí durante un largo
período de tiempo utilizando el método de BeginIdle.
A veces, se interrumpe la conexión,
causando una
MailBee.MailBeeAbortedByRemoteHostException
excepción. Aunque puedo ver el error antes de que
se produce utilizando el evento ErrorOccurred, no puedo
controlar la excepción (es decir, volver a conectar.) Por el contrario,
la aplicación se bloquea. El código que arroja
La excepción es en un hilo creado por el
Código MailBee.NET así que no puedo cogerlo.

¿Debo crear mi propio hilo que hace al ralentí
utilizando inactivo, o hay una manera de manejar la
excepción?
Back to Top View tromby24's Profile Search for other posts by tromby24
 

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump

Powered by Web Wiz Forums version 7.9
Copyright ©2001-2004 Web Wiz Guide