Before OLE can be invoked, the current thread must be set to single-threaded apartment (STA) mode

Before OLE can be invoked, the current thread must be set to single-threaded apartment (STA) mode

 
copy code
Before OLE can be invoked, the current thread must be set to single-threaded apartment (STA) mode. Make sure your Main function is marked with STAThreadAttribute. This exception is only thrown when a debugger is attached to the process.

     The program was running normally before, I just defined in an event to do the following operations, the program throws this exception

 


    private void OnExportPlan(object sender, EventArgs e)
        {
            try
            {
                if (null != ExportPlan)
                {
                    Cursor = Cursors.WaitCursor;
                    saveFileDialog1.Filter = "(Excel2003).xls|*.xls|(Excel2007).xlsx|*.xlsx";
                    if (DialogResult.OK == saveFileDialog1.ShowDialog())
                    {
                        mExportFilePath = saveFileDialog1.FileName;
                        Application.DoEvents();
                        ExportPlan(this, null);
                    }
                }
            }
            finally
            {
                Cursor =  Cursors.Default;
            }
        }


 Check MSDN, there are the following instructions:

 

  A unit is a logical container of objects within a process that have the same thread access requirements. All objects in the same apartment can receive calls from any thread in the apartment. The .NET Framework does not use units, the managed object itself is responsible for using all shared resources in a thread-safe manner.

  Because COM classes use units, the common language runtime needs to create and initialize a unit when a COM object is called in the case of COM interop. Managed threads can create and enter a single-threaded apartment (STA) that allows only one thread or a multi-threaded apartment (MTA) that contains one or more threads. You can control the type of apartment that is created by setting the thread's ApartmentState property to one of the ApartmentState enumeration values. Because a given thread can only initialize a COM unit once, the unit type cannot be changed after the first call to unmanaged code.

  Member definition:

     STA:Thread will create and enter a single-threaded apartment.

     MTA: Thread will create and enter a multithreaded apartment

  Unknown: The ApartmentState property has not been set.

There are two solutions:

1 ) Add the Attribute of STAThread at the Main entry

   


[STAThread]
    static void Main() 

 

2)

 


   Thread  app  =  new  Thread(new  ParameterizedThreadStart(ShowWindow));
   app.ApartmentState  =  ApartmentState.STA;

 //也可以
 Thread newThread = new Thread(new ThreadStart(ThreadMethod));
 newThread.SetApartmentState(ApartmentState.MTA);

 
Please indicate the original address for reprinting 
: http://www.cnblogs.com/winzheng/archive/2008/12/02/1345656.html

E.g:

 


 static void Main()
    {
        Thread newThread =
            new Thread(new ThreadStart(ThreadMethod));
        newThread.SetApartmentState(ApartmentState.MTA);

        // The following line is ignored since 
        // ApartmentState can only be set once.
        newThread.SetApartmentState(ApartmentState.STA);

        Console.WriteLine("ThreadState: {0}, ApartmentState: {1}", 
            newThread.ThreadState, newThread.ApartmentState);

        newThread.Start();

        // Wait for newThread to start and go to sleep.
        Thread.Sleep(300);
        try
        {
            // This causes an exception since newThread is sleeping.
            newThread.SetApartmentState(ApartmentState.STA);
        }
        catch(ThreadStateException stateException)
        {
            Console.WriteLine("\n{0} caught:\n" +
                "Thread is not in the Unstarted or Running state.", 
                stateException.GetType().Name);
            Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
                newThread.ThreadState, newThread.GetApartmentState());
        }
    }

    static void ThreadMethod()
    {
        Thread.Sleep(1000);
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326459754&siteId=291194637