8 Replies - 2500 Views - Last Post: 14 August 2013 - 12:24 AM Rate Topic: -----

#1 Anthonidas   User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 240
  • Joined: 25-April 11

outlook interop disables UI while the current mail item is displaying

Posted 08 August 2013 - 03:52 AM

Hi guys

In my application I use Outlook Interop to send emails. I have a mail button that prepares the desired email item and then displays it.

The problem is, that while my mail item is showing (outlook window is open, where i can write the message and so on), my whole application is disabled. I'll explain better with some code:

// Getting started with the mail item
// I am using the interop service with alias "Outlook"
Outlook.Application oApp = new Outlook.Application();

private void mailBtn_Click(object sender, EventArgs e)
{
    Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
        oMailItem.Subject = "Here comes my subject";
        oMailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
        oMailItem.HTMLBody = "This is my Emailbody... obviously with some html-tags. :P/>"

        oMailItem.Display(true); // HERE IT STUCKS UNTIL MY MAIL WINDOW IS CLOSED!
}



when i display my mail item it stucks there until my mail is sent or until i close the mail window. It is even not possible to drag my app window away. I know that it is due to the fact, that my button event has not finished yet but this is not useful because i have to check some other parts of my app to get the email ready. In this case it is not possible. What do you propose? Is there a possibility to do so?

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: outlook interop disables UI while the current mail item is displaying

#2 MrShoes   User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: outlook interop disables UI while the current mail item is displaying

Posted 08 August 2013 - 03:58 AM

Seems the problem could easily be solved by running the process in a separate thread. Try ThreadPool.QueueUserWorkItem() or a BackgroundWorker if you want some kind of reporting of progress. Even better, if you're using .NET 4.5, separate that code into a method and specify it as async.
Was This Post Helpful? 1
  • +
  • -

#3 Anthonidas   User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 240
  • Joined: 25-April 11

Re: outlook interop disables UI while the current mail item is displaying

Posted 08 August 2013 - 04:58 AM

Oh, you are right... I'll give it a try :)

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7187
  • View blog
  • Posts: 24,357
  • Joined: 05-May 12

Re: outlook interop disables UI while the current mail item is displaying

Posted 09 August 2013 - 08:38 PM

No. Don't run it on another thread. Outlook and its interop is inherently single threaded. They (the Outlook team) had to jump through hoops to get it partially multithreaded just within itself, and even then it's still not perfect. The interop story is much worse because it assume single threaded usage.

Have you tried the Activate method?

Quote

The Display method is supported for explorer and inspector windows for the sake of backward compatibility. To activate an explorer or inspector window, use the Activate method.


http://msdn.microsof...office.14).aspx
Was This Post Helpful? 0
  • +
  • -

#5 Anthonidas   User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 240
  • Joined: 25-April 11

Re: outlook interop disables UI while the current mail item is displaying

Posted 12 August 2013 - 03:06 AM

Hmmm... in fact I solved my problem with the ThreadPool.QueueUserWorkItem() and it works like a charm...

I'm not sure what you mean with the Activate method. As I said before, in my previous post, it stands in the middle of my mail-method and it does not go on, until i close the mail-item-window. So it is even not possible to invoke a "Activate" method, while the mail-item is displayed. Moving my mail-method into the threadpool, instead, lets my UI unblocked...
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7187
  • View blog
  • Posts: 24,357
  • Joined: 05-May 12

Re: outlook interop disables UI while the current mail item is displaying

Posted 12 August 2013 - 05:47 AM

To "invoke" a method is to call a method. In your line of code:
oMailItem.Display(true);


you are invoking the Display() method.

If you look in the registry, Outlook is registered as being Apartment threaded. See HKEY_CLASSES_ROOT\CLSID\{0006F03A-0000-0000-C000-000000000046}\InprocHandler32:ThreadingModel. This means that you can't freely create an object in one thread, and just pass the object pointer into another thread. So in the case of your code above, if you instantiated the oApp object or the oMailItem object in one thread, you are going against the COM rules of passing that object pointer to another thread and invoking the Display() method.
Was This Post Helpful? 1
  • +
  • -

#7 Anthonidas   User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 240
  • Joined: 25-April 11

Re: outlook interop disables UI while the current mail item is displaying

Posted 13 August 2013 - 06:48 AM

Ok... Now I understand that. But how would you do that in a different way? I mean... do you avoid to put them in a different thread because it is a rule or does it cause some internal issues? Can this cause some crashes or can it damage my DB? (this is all a bit new to me, that's why I ask)
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7187
  • View blog
  • Posts: 24,357
  • Joined: 05-May 12

Re: outlook interop disables UI while the current mail item is displaying

Posted 13 August 2013 - 02:44 PM

Simply don't cross threads with the Outlook automation object pointers. Do all your Outlook interop in another thread.
Was This Post Helpful? 1
  • +
  • -

#9 Anthonidas   User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 240
  • Joined: 25-April 11

Re: outlook interop disables UI while the current mail item is displaying

Posted 14 August 2013 - 12:24 AM

ah... ok... well this is what I actually did. I did not just "display" in a new thread. Perhaps this is why it worked well. ;)

Thanks to both.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1