Threading Support in InfoPath Projects Using the InfoPath 2003 Object Model

The COM objects accessed through the Microsoft.Office.Interop.InfoPath.dll, Microsoft.Office.Interop.InfoPath.SemiTrust.dll, and Microsoft.Office.Interop.InfoPath.Xml.dll interop assemblies installed by Microsoft Office InfoPath 2007 do not support calls made on multiple threads. This includes the interfaces for Microsoft XML Core Services (MSXML) 5.0 for Office objects that are wrapped by the Microsoft.Office.Interop.InfoPath.SemiTrust namespace (most of which have names that are prefixed with IXMLDOM) and all of the interfaces exposed by the Microsoft.Office.Interop.InfoPath.Xml namespace, none of which are thread-safe.

All calls made to these COM objects must be issued on a single thread. Managed code in an InfoPath project can create other threads to perform background work, but code running on threads other than the main thread cannot call into the InfoPath object models.

If your InfoPath managed code project uses multiple threads, you must be careful about sharing objects between threads. No references to the XML Document Object Model (DOM) or references to InfoPath objects should be shared between threads.

Making Asynchronous Calls to the InfoPath Object Model

For situations where it is necessary to call into a process such as a timer running on a separate thread, it is possible to work around the fact that the InfoPath object model does not support such calls.

The following example creates a System.Timers.Timer instance in the _Startup method of the form and hooks up an asynchronous callback to the timer. In addition, an invisible instance of the window form (System.Windows.Forms.Form) is created. When the timer elapsed callback function executes once per second, it calls into the Win32 PostMessage function to post a message to the invisible window. The invisible window has a WndProc function that processes the message it receives from the timer callback function, and updates the XML document object model (DOM) of the form. This form must be installed as a fully trusted form to run. For information on debugging a fully trusted form template, see How to: Preview and Debug Managed Code Form Templates that Require Full Trust. For information on deploying a fully trusted form template, see How to: Deploy InfoPath Projects.

using System;
using Microsoft.Office.Interop.InfoPath.SemiTrust;
using System.Timers;
using System.Runtime.InteropServices;

// Office integration attribute. Identifies the startup class for the
// form. Do not modify.
[assembly: System.ComponentModel.DescriptionAttribute("InfoPathStartupClass, Version=1.0, Class=AsyncUpdate.AsyncUpdate")]

namespace AsyncUpdate
    public class User32
        public static extern Int32 PostMessage(
            IntPtr hWnd, int Msg, int wParam, int lParam);

        public User32()


    public class MyWindow : System.Windows.Forms.Form
        private XDocument thisXDocument;
        private AsyncUpdate thisProcess ;

        // Private message for internal class.
        public const int WM_MYNOTIFY = 0x400;

        public MyWindow(XDocument doc, AsyncUpdate process)
            thisXDocument = doc;
            thisProcess  = process;

            this.Text = "MyWindow";

            // Force HWND to get created in Win32
            IntPtr hwnd = this.Handle; 

        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
        protected override void WndProc(
            ref System.Windows.Forms.Message m) 
            switch (m.Msg)
            case WM_MYNOTIFY:
                IXMLDOMNode xml = thisXDocument.DOM.selectSingleNode(
                xml.text = thisProcess.counter.ToString();
            base.WndProc(ref m);

    // The namespace prefixes defined in this attribute must remain 
    // synchronized with those in the form definition file (.xsf).
    public class AsyncUpdate
        private XDocument thisXDocument;
        private Application thisApplication;
        public int counter;
        private System.Timers.Timer myTimer;
        private MyWindow myWnd;
        public void _Startup(Application app, XDocument doc)
            thisXDocument = doc;
            thisApplication = app;

            // init the counter
            counter = 0;
            // Start a timer on another thread
            myTimer = new System.Timers.Timer(1000);
            myTimer.Elapsed += new ElapsedEventHandler(
            // create hidden window to receive notifications 
            // back on the main thread
            myWnd = new MyWindow(thisXDocument, this);

        public void _Shutdown()

        private void myTimer_Elapsed(object sender, ElapsedEventArgs e)
            // This method is called on a second thread
            counter ++;
            // Post message back to main thread
                myWnd.Handle, MyWindow.WM_MYNOTIFY, 0, 0);