Freelance Project Requests info@CodeDocu.de Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

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

 

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() >---------------

}

 

 

Mobile
»
Solved: Thread must be set to Single Thread Apartment (STA) mode
»
C #, Winforms: error CS1061: PropertyStore does not contain a definition for Settings

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de