Clipboard and STAThreadAttribute

  • Thread starter Thread starter Roy Gunnarsson
  • Start date Start date
R

Roy Gunnarsson

Hello all,

I have a VERY simple windows application that decrypts data files we receive
from a partner. The decryption is handled by a COM component our partner
provided us with (thus we have no control over its functionality). That,
however, works fine. To make it a little easier for our end users, I wanted
to include a feature that places the path and name of the decrypted file on
the clipboard. I was going to do this by using
Clipboard.SetDataObject(sNamePath). However when I run the application, I
receive an error stating:

"The current thread must set to Single Thread Apartment (STA) mode before
OLE calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it."

The error occurs on the Clipboard.SetDataObject line. I don't have a 'Main'
function (I tried putting a Main sub in with a Application.Run(Form1) and
mark it with the <STAThreadAttribute> but with no change).

I built another windows app just to test the Clipboard.SetDataObject and it
worked just fine...

I would be very grateful for some insight. I don't absolutely need this
feature, it just bugs me that it's not working and I don't know why.

Thanks,

Roy Gunnarsson
 
Roy,
What kind of application is this? (Windows Forms, Windows Service, Console)

Are you attempting to use any async processing? (Begin*, ThreadPool)

VB.NET by default will make your main form STA, you should not need to add
it.

Is your program a single form, can you post a simple example that has the
problem, preferable on a website, however if its a small zip file you can
attach it here.

Hope this helps
Jay
 
You are calling Application.Run from main sub. Take a good look at the code.
That's where you have to set STA model.
Also check project properties, if I remember correctly, there is something
there related to STA/MTA too.

HTH
Alex
 
Back
Top