469,963 Members | 1,317 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

(Urgent)Regarding : STAThreadAttribute

Hi there,
I want to copy a Image to the clipboard. When i try to do so following
exception occurs :
"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"
Following are the points i want to say :
1. Which is the main method in windows forms ? When i tried to assign
<STAThread()> attribute to Public sub New () it gave me error.
2. I tried adding module contaning public sub main() assigning <STAThread()>
attribute, but above exception continues.
3. When i am checking the Threading.Thread.CurrentThread.ApartmentState
property in the function where exception is there , it is showing me the
value 1.
4. In my whole application I have never used Threads manually.But it does
contain many events.
Application i am working on is based on Client Server Architecure and
the code which gives exception is written on Server Side.This code gets
executed when request is received from the client.

Plz give me suggestions, its very URGENT.
Thanks in advance.

Chandrakant Shinde
Nov 20 '05 #1
2 7727
100
Hi Chandrakant,
If you use VB your application's main thread runs in STA mode.
However, if you get *1* from Threading.Thread.CurrentThread.ApartmentState
that means MTA mode.
If you copy the immage in the clipboard as a response of an event the
corresponding event handler may be executed in separate thread from the
thread pool. Make sure you don't use somewhere BeginInvoke method.

If you don't have control on how the events are fired what you can do is to
call Control.Invoke to switch back to the main thread and then copy the
image.
HTH
B\rgds
100
"Chandrakant Shinde" <sh******@hotmail.com> wrote in message
news:uR**************@TK2MSFTNGP09.phx.gbl...
Hi there,
I want to copy a Image to the clipboard. When i try to do so following
exception occurs :
"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"
Following are the points i want to say :
1. Which is the main method in windows forms ? When i tried to assign
<STAThread()> attribute to Public sub New () it gave me error.
2. I tried adding module contaning public sub main() assigning <STAThread()> attribute, but above exception continues.
3. When i am checking the Threading.Thread.CurrentThread.ApartmentState
property in the function where exception is there , it is showing me the
value 1.
4. In my whole application I have never used Threads manually.But it does
contain many events.
Application i am working on is based on Client Server Architecure and
the code which gives exception is written on Server Side.This code gets
executed when request is received from the client.

Plz give me suggestions, its very URGENT.
Thanks in advance.

Chandrakant Shinde

Nov 20 '05 #2
Thanks 100,
Your Idea worked !!!!!

Chandrakant Shinde
----------------------------------------------------------------

"100" <10*@100.com> wrote in message
news:Or**************@tk2msftngp13.phx.gbl...
Hi Chandrakant,
If you use VB your application's main thread runs in STA mode.
However, if you get *1* from Threading.Thread.CurrentThread.ApartmentState
that means MTA mode.
If you copy the immage in the clipboard as a response of an event the
corresponding event handler may be executed in separate thread from the
thread pool. Make sure you don't use somewhere BeginInvoke method.

If you don't have control on how the events are fired what you can do is to call Control.Invoke to switch back to the main thread and then copy the
image.
HTH
B\rgds
100
"Chandrakant Shinde" <sh******@hotmail.com> wrote in message
news:uR**************@TK2MSFTNGP09.phx.gbl...
Hi there,
I want to copy a Image to the clipboard. When i try to do so following
exception occurs :
"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"
Following are the points i want to say :
1. Which is the main method in windows forms ? When i tried to assign
<STAThread()> attribute to Public sub New () it gave me error.
2. I tried adding module contaning public sub main() assigning

<STAThread()>
attribute, but above exception continues.
3. When i am checking the Threading.Thread.CurrentThread.ApartmentState
property in the function where exception is there , it is showing me the value 1.
4. In my whole application I have never used Threads manually.But it does contain many events.
Application i am working on is based on Client Server Architecure and the code which gives exception is written on Server Side.This code gets
executed when request is received from the client.

Plz give me suggestions, its very URGENT.
Thanks in advance.

Chandrakant Shinde


Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by | last post: by
10 posts views Thread by Dhananjay | last post: by
reply views Thread by gsreenathreddy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.