开发者

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 func开发者_Python百科tion 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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜