472,146 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

(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 7822
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
reply views Thread by Saiars | 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.