简体   繁体   中英

System.Threading.ThreadstateException

I'm developing an adding for office powerpoint application. I'm trying to display a description of the object(Customized object) currently dropped on the powerpoint slide in design mode(Design mode of the powerpoint). When i click on my addin the related object description will be displayed on a tabbed window as the first tabpage.

There is a button on the tab page, and when i click on it i need the description to get copied to windows clipboard. I tried this using clipboardclass it throws the following exception, System.Threading.ThreadstateException {"Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it."}

Code for clipboard: Clipboard.Clear() Clipboard.SetText(lblObjectID.Text)

I searched the net for a solution and got couple of answers like, 1. Put [STAThread] in the main function 2. Thread.CurrentThread.SetApartmentState(ApartmentState.STA) Immediately before your call to SetDataObject.

But I'm not sure where to put the 1st one and the 2nd option didn't work. Can anyone help me please. Thanks.

WinForms are STA by default. Are you creating another thread or using a BackgroundWorker? Run this code to determine what mode you're in:

MessageBox.Show(System.Threading.Thread.CurrentThread.GetApartmentState().ToString())

Edit:

But maybe you could also try using this command before calling the clipboard functions:

Application.OleRequired()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM