473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 5224
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Wayne Wengert | last post by:
I have an aspx page on which I am trying to copy the contents of a textbox to the client clipboard when the users clicks a button. The button code is as follows:...
9
by: Tim Frawley | last post by:
I have converted a VB6 application to VB.NET. The old application made extensive use of the Clipboard for copying an Image Name so that it could be pasted into the image capture app when the user...
3
by: SAL | last post by:
I have the following VB.net Class/Function that I call from an ASP.net page: Public Class MyTestClass Public Function embedHyperlink(ByVal fileID As Integer, ByVal fileName As String) As String...
0
by: renfes | last post by:
Hey everyone. I have a program that watches a .txt log file from a chat client or a game and based on each line, triggers events. One thing I am trying to do is send the contents of a text file to...
1
by: Sefner | last post by:
Hey everyone. I have a program that watches a .txt log file from a chat client or a game and based on each line, triggers events. One thing I am trying to do is send the contents of a text file to...
5
by: Sin Jeong-hun | last post by:
class Engine { Thread Worker; public event ... EngineMessage; public void Start() { Worker=new Thread(new ThreadStart(Run)); Worker.Start(); } private Run()
2
by: Curious | last post by:
I got the error when I execute the following line of code: Clipboard.SetDataObject(mGrid.Clip); The error is: "Current thread must be set to single thread apartment (STA) mode before OLE...
2
by: asmapanjabi | last post by:
am using C#.net 1.1 while using clipboard m geeting this exception System.Threading.ThreadStateException: Current thread must be set to single thread apartment (STA) mode before OLE calls can be...
5
by: Max2006 | last post by:
Hi, No matter what I have in clipboard Clipboard.GetDataObject(); returns null. I suspect it might be a problem with Vista or VS2008 IDE debug mode problem? Is there any way to track why the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.