none
How to run background task in Outlook 2010 add-in efficiently? RRS feed

  • Question

  • i am developing a outlook addin in c#, heres my startup:

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            //adds the new issueTopMenu
            //Search the menu and delete if found
            RemoveMenubar();
    
            //adds the panel
            AddPanelToExplorer();
    
            //Method to create new menu
            AddMenuBar();
        }

    How can i do it to run on a thread, because it access to a webservice to get some data, and when outlook start it freezes until it obtain the data, and i want to eleminate that freeze time.

    • Moved by CoolDadTx Friday, February 6, 2015 3:36 PM Office related
    Friday, February 6, 2015 5:20 AM

Answers

  • Hi Vishva,

    Base on my test in outlook 2013, I can access mail item or change property in different thread.

    private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                ThreadStart s = new ThreadStart(NewThreadTest);
                Thread ss = new Thread(s);
                ss.Start();
            }
            void NewThreadTest()
            { 
                Thread.Sleep(new TimeSpan(0, 0, 10));
               
                Globals.ThisAddIn.GetMessage();
               }
    internal void GetMessage()
           {
    
               Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
               Outlook.MailItem currentOpenedMail = inspector.CurrentItem as Outlook.MailItem;
               string o = currentOpenedMail.Subject;
               currentOpenedMail.To = "XXX";
               return;
          }

    So, for multithreading in outlook add-in, that base on the detail requirement. I suggest that you could do the test in the add-in for outlook 2010, if you have the issue during the test, you could post the related code here or open a new thread.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 9, 2015 9:35 AM
    Moderator

All replies

  • using System.Threading.Tasks;
    using System.Threading;
    
    namespace Whatever
    {
    	public partial class class1
    	{
    	private Thread Oworker = null;
            private void appLoad_Load(object sender, EventArgs e)
    		{
                    //your programs load event whatever that may be
                    this.Oworker = new Thread(new ThreadStart(this.ThisAddIn_Startup));
    	        this.ThisAddIn_Startup.Start();
    		}
    
    	private void ThisAddIn_Startup(object sender, System.EventArgs e)
        		{
            	//adds the new issueTopMenu
            	//Search the menu and delete if found
            	RemoveMenubar();
    
            	//adds the panel
            	AddPanelToExplorer();
    
            	//Method to create new menu
            	AddMenuBar();
        		}
    	}
    }
    Thrown together on the fly but should work

    Friday, February 6, 2015 9:39 AM
  • this article contains good information for you:

    http://www.codeproject.com/Articles/93288/Add-in-Programming


    Mark as answer or vote as helpful if you find it useful | Ammar Zaied [MCP]

    • Proposed as answer by GuruOne Tuesday, February 10, 2015 1:18 PM
    Friday, February 6, 2015 9:58 AM
  • Thanks for reply,

    Actually, I have developed an ADD-in for outlook 2010. On outlook startup, ADD-in stored all the mail subjects in SQLite database and than windows services also access same database, and post it on rest-based services. 

    Is there are substitute of Redemption. I have to use same outlook object in different thread simultaneously? 


    Friday, February 6, 2015 10:42 AM
  • yes you can use the same outlook object in different thread simultaneously,but i don't no if this solution contains disadvantage !

    we can search to solve the problem.


    Mark as answer or vote as helpful if you find it useful | Ammar Zaied [MCP]

    Friday, February 6, 2015 11:00 AM
  • Thanks for your Quick reply

    @A.Zaied As per my knowledge outlook work on a single thread when we use the main thread for other process, outlook freezes.

    please suggest me how can we implement it. 

    Friday, February 6, 2015 12:00 PM
  • Hello Vishva,

    You need to use the Outlook object model on the main thread only.

    Outlook like most Office applications uses the Single Threaded Apartment (STA) model which doesn't allow to work with multiple threads. All cross-thread calls will be marshalled to the single thread by Outlook. Use only auxiliary methods on another threads. For example, you may run a service call to retrieve the data on a secondary thread.

    But you can use a low-level API - Extended MAPI which allows to run calls from secondary threads. Therefore, Redemption, which is just a wrapper around that API, supports secondary threads. You just need to initialize it with an existing session object. See I am using Redemption objects on multiple threads. Is there anything I need to be aware of? for more information.

    Friday, February 6, 2015 3:44 PM
  • Vishva,

    You are absolutely right. It seems Ammar doesn't know anything about Outlook programming.


    Friday, February 6, 2015 3:46 PM
  • Hi Vishva,

    Base on my test in outlook 2013, I can access mail item or change property in different thread.

    private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                ThreadStart s = new ThreadStart(NewThreadTest);
                Thread ss = new Thread(s);
                ss.Start();
            }
            void NewThreadTest()
            { 
                Thread.Sleep(new TimeSpan(0, 0, 10));
               
                Globals.ThisAddIn.GetMessage();
               }
    internal void GetMessage()
           {
    
               Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
               Outlook.MailItem currentOpenedMail = inspector.CurrentItem as Outlook.MailItem;
               string o = currentOpenedMail.Subject;
               currentOpenedMail.To = "XXX";
               return;
          }

    So, for multithreading in outlook add-in, that base on the detail requirement. I suggest that you could do the test in the add-in for outlook 2010, if you have the issue during the test, you could post the related code here or open a new thread.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 9, 2015 9:35 AM
    Moderator