MailItem.Close()

  • Thread starter Thread starter Bina
  • Start date Start date
B

Bina

Is MailItem.Close() a synchronous or asynchronous operation? In other words,
if I first close my MailItem and then call Delete() on it, should this work?

Bina
 
Calling Close on your MailItem closes the item, but does not destroy your
reference to it. You can therefore call Delete thereafter.
 
I am concerned about whether the call to Close() is a synchronous or
asynchronous operation. If I am displaying an email, and I call the Close()
function, and it is asynchronous, I may be trying to Delete the item while it
is still being displayed.

The background for my question is that I have created a temporary folder
with an email in order to display it. I want to Close() the email and delete
the folder and the email. However, sometimes (not in all cases) the delete
operation fails with a permissions error. If I wait a while and then delete,
or step through this process in a debugger, it never fails. So, I was
wondering if Close() is actually an asynchronous operation. If so, is there
a way to know when it has completed?
 
I do not know for certain if it is synchronous or not, but I suspect that it
is. Can you post the relevant section of your code. I suspect that you are
under certain circumstances not setting the mailitem's object variable back
to nothing before deleting the folder. Also, calling the mailitem's delete
method invokes the inspector to close anyway, so you probably do not need to
call it separately.
 
Close is synchronous but you can't delete the item in that event handler
without errors in some versions of Outlook.

Outlook also caches items so you have to wait for it to be released.

I usually use a timer to do the deletions and set up an array of EntryID's
of items to delete. When the timer fires I sweep the array clean.
 
Hello,

What we have done at work is to create a thread

In the Inspector Clos event :

If RemoveItemAfterClose Then
Dim TS As New System.Threading.ThreadStart(AddressOf
WrapperMailItem.Delete)
Dim t As System.Threading.Thread = New System.Threading.Thread(TS)
Try
t.Start()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If

Works like a charm
 
Just make sure you do not access the Outlook object model on that thread or
if you do that you first marshal the thread to the main thread in your code.
Otherwise you not only crash or hang Outlook but also break any other addin
that's running.

If you cannot meet those conditions then your solution will cause problems,
not fix them.
 
Back
Top