472,139 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,139 software developers and data experts.

Clipboard and STAThreadAttribute

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
2 5145
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
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.

Similar topics

4 posts views Thread by Wayne Wengert | last post: by
3 posts views Thread by SAL | last post: by
5 posts views Thread by Sin Jeong-hun | last post: by
2 posts views Thread by Curious | last post: by
5 posts views Thread by Max2006 | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.