By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,442 Members | 1,312 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,442 IT Pros & Developers. It's quick & easy.

Clipboard and STAThreadAttribute

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

"Roy Gunnarsson" <rg*******************@excelsior.edu> wrote in message
news:OG**************@TK2MSFTNGP09.phx.gbl...
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

Nov 20 '05 #2

P: n/a
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

"Roy Gunnarsson" <rg*******************@excelsior.edu> wrote in message
news:OG**************@TK2MSFTNGP09.phx.gbl...
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

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.