Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Solved: Thread must be set to Single Thread Apartment (STA) mode

14.11.2018 (👁10858)


 

Problem:

When using the Clipboard clipboard in an Addin application for Word comes the message that the clipboard is not declared as STA thread.

 

Source code with error:

Currently in the program a reduced bitmap is created from a photo, which should then be placed in the clipboard.

Image outImage = new Bitmap(source_Bitmap, new_Width, new_Height);

Clipboard.SetImage(outImage); //*Error with STA

 

Solution:

You have to open a new thread for the operation and run it with Start and Join.

Here in the code-Besipiel a new thread is created and executed.

new Thread((ThreadStart)(() => { Problem-Code }));

 

Complete code example

Please note: the thread must be set as SetApartmentStart with STA and then started with .Start.

    Image outImage = new Bitmap(source_Bitmap, new_Width, new_Height);

    var t = new Thread((ThreadStart)(() =>

    {

        Clipboard.SetImage(outImage);

    }));

    t.SetApartmentState(ApartmentState.STA);

    t.Start();

    t.Join();

 

 

 

 

Subject:

C # in Windows Forms, WPF, Calls Clipboard in VSTO Word Intercom Addin

 

Error message:

Message = For the current thread, the Single Thread Apartment (STA) mode must be set before OLE calls can be made.

Make sure that the main function is marked with STAThreadAttribute.

 

 

 

System.Threading.ThreadStateException

  HResult=0x80131520

  Message=Für den aktuellen Thread muss der STA-Modus (Single Thread Apartment) festgelegt werden, bevor OLE-Aufrufe ausgeführt werden können. Stellen Sie sicher, dass die Hauptfunktion mit STAThreadAttribute gekennzeichnet ist.

  Source=word_Addin_03_Photos

  StackTrace:

   at Addin_Word_Photos_into_Table.Ribbon1.load_Photo_compressed_to_Clipboard(String input_Image_Path, Int32 setLength) in ..

 

 

Solution:

 

Example code

With

public static void load_Photo_compressed_to_Clipboard(string input_Image_Path, int setLength)

{

    //---------------< Image_resize() >---------------

    Bitmap source_Bitmap = new Bitmap(input_Image_Path);

 

    double dblWidth_origial = source_Bitmap.Width;

    double dblHeigth_origial = source_Bitmap.Height;

 

    //< check orientation >

    bool IsOrientation_Horizontal = (dblWidth_origial > dblHeigth_origial) ? true : false;

    //</ check orientation >

 

    int new_Height = 0;

    int new_Width = 0;

    double zoom_factor = 1;

    if (IsOrientation_Horizontal == true)

    {

        new_Width = setLength;

        zoom_factor = new_Width / dblWidth_origial;

        new_Height = (int)(dblHeigth_origial * zoom_factor);

    }

    else

    {

        new_Height = setLength;

        zoom_factor = new_Height / dblHeigth_origial;

        new_Width = (int)(dblWidth_origial * zoom_factor);

    }

 

    Image outImage = new Bitmap(source_Bitmap, new_Width, new_Height);

    var t = new Thread((ThreadStart)(() =>

    {

        Clipboard.SetImage(outImage);

    }));

    t.SetApartmentState(ApartmentState.STA);

    t.Start();

    t.Join();

 

    //---------------</ Image_resize() >---------------

}