1

I'm currently playing around with Office 2013 Add-Ins with Visual Studio 2013. I've created a Ribbon with a Button that displays a little Windows Form:

        private void outterMailCreateNewFaxBTN_Click(object sender, RibbonControlEventArgs e)
    {
        CreateNewFax cnf = new CreateNewFax(this);
        cnf.Show();
    }

When the user clicks on one of the buttons on the Form, a new MailItem is being created (with some information inside it).

private void button1_Click(object sender, EventArgs e)
    {
        this.Dispose();
        this.outterMailRibbon.setFaxNumber(faxNumber, this);
    }

Here is the setFaxNumber-Method:

public void setFaxNumber(String faxNumber, CreateNewFax cnf)
    {
        cnf = null;
        //mother.Dispose();
        this.faxNumber = faxNumber;

        Outlook.Application application = Globals.ThisAddIn.Application;
        Outlook.MailItem myMailItem = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);

        myMailItem.To = this.faxNumber;
        myMailItem.Subject = "[FAX:" + this.faxNumber + "]";
        myMailItem.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
        ((Outlook.ItemEvents_10_Event)myMailItem).Send += new Microsoft.Office.Interop.Outlook.ItemEvents_10_SendEventHandler(ThisAddIn_Send);
        this.gMailItem = myMailItem;
        myMailItem.Display(true);

    }

The problem im currently facing is that after the user clicked on the specific button on the Form the Outlook main-process is blocked and the UI freezes until the newly created Message is sent or discarded.

How can I avoid this behaviour (so that the user is able to display other Mails from outlook, while this newly created MailItem is still editable by the user)?

EDIT: The Outlook-UI freezes after the MailItem was created. At the Time, the Windows Form is still opened, Im able to use the Outlook-UI as usual.

Daniel M
  • 177
  • 1
  • 1
  • 13
  • Isn't that because you're creating a `Send` method? So that, when you create a mail, you also create a method, so it waits you go through it? I'm not sure it's that, but thats an idea. – provençal le breton Jul 10 '14 at 07:59
  • Is the whole outlook window freezing? Does it stay frozen until the task is completed? – Alex Jul 10 '14 at 08:21
  • @AlexHarvey : Yes, the whole Outlook is frozen until the MailItem I created was sent. – Daniel M Jul 10 '14 at 08:23
  • @Zaphod : I just detached the Send-Event - same behaviour. Outlook freezes until i have sent the MailItem. – Daniel M Jul 10 '14 at 08:52

1 Answers1

2

EDIT:

Try changing myMailItem.Display(true); with myMailItem.Display(false); as the the MailItem.Display documentation states that false is the default modal parameter. If set to true outlook will freeze until its ready.


OLD ANSWER (In most UI freezing situations this will fix the problem, however this case was different) :

By going on what you have said it sounds like the send mail method is holding up the main thread until it is sent off, this will cause the main UI to freeze. The best way to deal with a freezing UI is to thread the method or sometimes you could use a dispatcher timer and put the method inside the tick.

I am not sure if this would work as I am writing from memory but if you were to thread the function you could do it like this:

    var thread = new Thread( () =>
    {
        this.outterMailRibbon.setFaxNumber(faxNumber, this);
    });

    thread.Start();
    thread.Join(); // The thread will auto leave and close once the execution is complete

The above code will execute the setFaxNumber method in a new thread separate from the main thread which the UI runs on, this means it will not hold up any UI loading.

If you need any more info just ask and I can add it to my answer :)

Alex
  • 1,052
  • 7
  • 19
  • Calling the setFaxNumber-Method this way still blocks the Outlook-UI :( – Daniel M Jul 10 '14 at 09:22
  • I dont know exactly. I would say somewhere in the button1_Click-Method. Could you tell me, how i could track it down to a specific line? – Daniel M Jul 10 '14 at 09:40
  • Trying commenting out the this.Dispose(); line and see if the UI hangs still and for as long – Alex Jul 10 '14 at 09:46
  • Uncommented that line (and still using the threaded call) - but the Outlook-UI still freezes until i have sent (or discarded) the MailItem. – Daniel M Jul 10 '14 at 09:48
  • 2
    Try replacing myMailItem.Display(true); with myMailItem.Display(false); – Alex Jul 10 '14 at 10:11
  • Brilliant. I will update the answer with some more information to explain why. – Alex Jul 10 '14 at 10:16